Dynamic html execution via javascript load

Discussion in 'JavaScript' started by News Updates, Nov 5, 2012.

  1. #1
    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.
     
    Solved! View solution.
    News Updates, Nov 5, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    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.)
     
    Rukbat, Nov 5, 2012 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    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.
     
    deathshadow, Nov 5, 2012 IP
  4. News Updates

    News Updates Member

    Messages:
    490
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    35
    #4
    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/
     
    Last edited: Nov 5, 2012
    News Updates, Nov 5, 2012 IP
  5. #5
    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?
     
    Last edited: Nov 5, 2012
    deathshadow, Nov 5, 2012 IP
  6. News Updates

    News Updates Member

    Messages:
    490
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    35
    #6
    I was just testing with fwd slash back slash escaping.

    thanks, its working.
     
    Last edited: Nov 5, 2012
    News Updates, Nov 5, 2012 IP