Hello guys, I need a little hand with javascript. I've been trying for hours but couldn't. I want to execute a piece of html via javascript in the class. below is the raw javasript code <html> <head> <script> var n = encodeURIComponent({url}); var m = encodeURIComponent({videolink}); <a href="http://mysite.com/button/?url=' + n + "&media=" + m + '" class="submit media"><img border="0" src="//button.png"/></a> </script> </head> <body> <div class="post-submit"></div> </body> </html> Code (markup): I want to execute the <a href> part in the div <post-submit class. Why Im doing it because I need to achieve the encodeURIComponent in submission link.
You can't "execute" HTML, since it's content, not program. You can send the user to that URL if that's what you want to do, by using window.location = "http://mysite.com/button/?url=" + n + "&media=" + m or whatever the URL is. If that's not what you want "executing the HTML" to do, post a better explanation. (It's much easier to have PHP create the page, creating whatever link or data you want wherever you want it.)
You can't just use HTML inside a SCRIPT tag -- once you open <SCRIPT> the ONLY thing you can enter is javascript until the next </SCRIPT> To make javascript output HTML, you have to tell it to do so with 'document.write' thus: <script> var n = encodeURIComponent({url}); var m = encodeURIComponent({videolink}); document.write('<a href="http://mysite.com/button/?url=' + n + '&media=' + m + '" class="submit media"><img src="//button.png" alt="describe this" /></a>'); </script> Code (markup): Though really generating this type of data client side is an accessibility train wreck, and should be handled on the server BEFORE the page is even sent to users. BTW, BORDER is one of the many HTML attributes that has no business being used on websites after 1998.
thanks for guidance. I've tried like this but it doesn't appear working http://jsfiddle.net/eFvnH/1/ If I remove the following part of script. the result appears. otherwise nothing var n = encodeURIComponent(http://example.com); var m = encodeURIComponent(http://example.com/abc.jpg); Code (markup): another method: actually its the same as above but I made it to get result out of javascript. http://jsfiddle.net/yUjhG/
Those are strings, you have to put them in quotes. var n = encodeURIComponent('http://example.com'); var m = encodeURIComponent('http://example.com/abc.jpg'); You are also using quotes in your document.write in a completely nonsensical order and escaping quotes for no good reason... mostly seeming like you're confusing where your doubles and singles belong... and back-slash escaping characters (like forward slash and double quotes inside singles) that don't need escaping. var n = encodeURIComponent('http://example.com'); var m = encodeURIComponent('http://example.com/1.jpg'); document.write('<a href="http://mysite.com/button/?url=' + n + '&media=' + m + '" >my link</a>'); Code (markup): It needs to be EXACTLY that. Though the {} stuff -- is that some sort of templating nonsense? If so and that's server side, what the devil are you using javascript for?