www.webdeveloper.com
Results 1 to 9 of 9

Thread: Problem with innerHTML...

  1. #1
    Join Date
    Jun 2010
    Posts
    11

    Problem with innerHTML...

    Hi again

    after I done my previous script (with a little big help of Padonak & Kor)

    I'm asking for help with other Javascript

    well I want to add some html code to a DIV, and the code includes a link obtained with Javascript.

    The problem is when I execute the script the page goes to another with only the html code that I had add and the code present in the DIV where the code was added.

    javascript:URL = document.getElementById("IMG to get URL").getElementsByTagName("img")[0].src; document.getElementById("DIV to add the HTML code...").innerHTML += '<center><input type="button" value="text button" onClick="window.open("URL","popup title","height=400, width=200");"></input></center>';


    what's wrong with this?? , and why the page gets white and only with the DIV where the code was been added...

    Please help me out
    Diogo Ribeiro

  2. #2
    Join Date
    Jan 2005
    Location
    Frankfort, KY, USA
    Posts
    6,167
    Rather than placing this inline, I suggest you create a function and call this in the link tag.
    Also, why do you have "URL" in the javascript string? Shouldn't it be "onclick"?

  3. #3
    Join Date
    Jun 2010
    Posts
    11
    Quote Originally Posted by KDLA View Post
    Rather than placing this inline, I suggest you create a function and call this in the link tag.
    Also, why do you have "URL" in the javascript string? Shouldn't it be "onclick"?
    it gets the url: document.getElementById("IMG to get URL").getElementsByTagName("img")[0].src

    and this should add the HTML code to the page but it redirects to a page with only the DIV I want to add the HTML code...:
    document.getElementById("DIV to add the HTML code...").innerHTML +=

    yap, I'm using in line because I'll add this script to a favorite of my browser, thats why I'm doing in line...

  4. #4
    Join Date
    Jun 2010
    Posts
    11
    I made some progress in the script...

    javascript:void(document.getElementById("DIV to add the code").innerHTML += '<script type="text/javascript">var URL=document.getElementById("DIV to get image URL").getElementsByTagName("img")[0].src;</script><center><input type="button" value="button text" onClick="javascript:window.open("URL","popup", "height=400, width=200");"></input></center>' );

    the problem now is with the link of the button :S which doesn't work

    maybe is a error with the javascript code creating the variable URL, IDK...

  5. #5
    Join Date
    Jun 2010
    Posts
    11
    please someone give me a help

  6. #6
    Join Date
    Jul 2010
    Location
    Sofia, Bulgaria
    Posts
    49
    Quote Originally Posted by Di.r View Post
    javascript:void(document.getElementById("DIV to add the code").innerHTML += '<script type="text/javascript">var URL=document.getElementById("DIV to get image URL").getElementsByTagName("img")[0].src;</script><center><input type="button" value="button text" onClick="javascript:window.open("URL","popup", "height=400, width=200");"></input></center>' );
    Where do you put that? In an onclick attribute? Elsewhere? Would you paste whole tag please?

  7. #7
    Join Date
    Jun 2010
    Posts
    11
    Quote Originally Posted by MGenev View Post
    Where do you put that? In an onclick attribute? Elsewhere? Would you paste whole tag please?
    I'll put this script in the browser address bar....

    if think that is wrong:

    onClick="javascript:window.open("URL","popup", "height=400, width=200");"

    maybe this is the best way:

    onClick="window.open("URL","popup", "height=400, width=200")"

    will be a problem with the tag?

    because I'm closing the onClick with the " in window.open("....

  8. #8
    Join Date
    Jul 2010
    Location
    Sofia, Bulgaria
    Posts
    49
    The first problem is with quotes inside the strings - your "main" string is single-quoted ('<script...'), inside of which there are double-quoted strings (like "DIV to add the code", "img" etc.), but the last of those ("window.open...") contain another sub-strings ("popup", "height...") which can't be specified without escaping their quote characters.

    The other problem is with the "URL" part - you're providing "URL" as the string "URL", not the value of the URL variable. This should work fine:
    Code:
    document.getElementById("DIV to add the code").innerHTML += '<script type=text/javascript>
    var URL=document.getElementById("DIV to get image URL").getElementsByTagName("img")[0].src;</script><center>
    <input type="button" value="button text" onclick="javascript:window.open(URL,\'popup\', \'height=400, width=200\');" />
    </center>';
    And few more things (about the generated HTML): it's better to use "onclick" instead of "onClick", and the proper would be "<input />" rather than "<input>", but those last aren't so important in your case.
    Last edited by MGenev; 07-12-2010 at 04:15 PM.

  9. #9
    Join Date
    Jun 2010
    Posts
    11
    Quote Originally Posted by MGenev View Post
    The first problem is with quotes inside the strings - your "main" string is single-quoted ('<script...'), inside of which there are double-quoted strings (like "DIV to add the code", "img" etc.), but the last of those ("window.open...") contain another sub-strings ("popup", "height...") which can't be specified without escaping their quote characters.

    The other problem is with the "URL" part - you're providing "URL" as the string "URL", not the value of the URL variable. This should work fine:
    Code:
    document.getElementById("DIV to add the code").innerHTML += '<script type=text/javascript>
    var URL=document.getElementById("DIV to get image URL").getElementsByTagName("img")[0].src;</script><center>
    <input type="button" value="button text" onclick="javascript:window.open(URL,\'popup\', \'height=400, width=200\');" />
    </center>';
    And few more things (about the generated HTML): it's better to use "onclick" instead of "onClick", and the proper would be "<input />" rather than "<input>", but those last aren't so important in your case.
    yap, there is a problem with the URL, I changed the variable "URL" to "_URL" and happened the same

    javascript:void(document.getElementById("DIV to add the code...").innerHTML += '<script type=text/javascript>
    var iohwe=document.getElementById("Image URL").getElementsByTagName("IMG")[0].src;</script><center>
    <input type="button" value="button text" onclick="javascript:window.open(iohwe,\'popup\', \'height=400, width=200\');" />
    </center>' );


    how could I get the link to the image and open the image in the popup?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles