SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamically outputing JavaScript

    Is the following possible?

    <html>
    <head>
    <script language="javascript" type="text/javascript">
    //this is my main script in my document

    var strAddScript = "<script language=\"javascript\" type=\"text/javascript\">";
    strAddScript += "function doThis(parameter) {";
    strAddScript += "//do really cool stuff here";
    strAddScript += "}";

    document.getElementById("AddScriptHere").innerHTML = strAddScript;

    </script>
    </head>
    <body>
    Here's my content
    <div id="AddScriptHere"></div>
    </body>
    </html>
    <!-- the above is a simplified version of a problem I am having. I can't seem to dynamically add JavaScript to a div using the innerHTML property. Is there another property I can use? Can I even add JavaScript dynamically to a div? If not, can I add JavaScript dynamically at all? If so How? -->

  2. #2
    SitePoint Guru okrogius's Avatar
    Join Date
    Mar 2002
    Location
    US
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't have a <script> tag within another <script>. Try using this little work-around - instead of setting a variable equal to '<script ....' set it equal to '<s'+'cript...'. Use the same for the closing tag.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you may have missed my point. I don't have a script tag within a script tag. I declare a variable that holds a bit of javascript, then attempt to output it as the html for a div in my document.

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Codename49 is correct.

    Now, before I move on, let me ask you this: WHY?

    In your example, you are attempting to add a script block which contains a function to the inside of a DIV. From a functional OR practical standpoint, this serves no benefit.
    Last edited by beetle; Oct 23, 2002 at 17:21.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's why:
    The site consists of modules which are dynamically loaded into a div on the index page. Some modules have thier own Javascript, some do not. The modules which do not have thier own javascript load fine, but the modules which contain their own script won't load the javascript. So I'm trying to extract the javascript from those modules (which I can do), and assign it back into the main page, (which I cannot).

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    provided that these 'modules' that DO contain javascript have no conflicting variable or function names between eachother, just put ALL that javascript into an external JS file and link it up to your index page.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did something like that. I have a dummy script tag on the index file which I dynamically change the src attribute on when needed.

    Thanks.


Bookmarks

Posting Permissions

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