Results 1 to 2 of 2

Thread: Assign Javascript to innerHTML

Hybrid View

  1. #1
    Join Date
    Aug 2009

    Assign Javascript to innerHTML


    Part 1: Problem: This line does not print the image link.
    Part 2: This line does print the image link.

    Both parts generate the same source code, although the first one does not print the image link. Why?
    (However the source code for the first part seems correct when I highlight div-section and click 'View Selection Source', but not if I click outside the div and choose 'View source'. why?)


    <SCRIPT LANGUAGE="JavaScript">
    var destination="textDestination";
    function showText(){
    if (document.getElementById){
    var dest=document.getElementById(destination);
    if (dest){ // && dest.innerHTML)

    //Part 1: Problem: This line does not print the image link.
    dest.innerHTML = '<script type="text/javascript">document.write(\'<a href="http://www.google.com" target="_BLANK"><img src="http://t2.gstatic.com/images?q=tbn:ANd9GcQV7hsgG998z8l7T5pyok6xqJFVqFmXlMAKmBFZQ0n06eLOEtojQQE1qwGjYQ" border=5></a>\');</script>';


    <title>JavaScript Typing Effect</title>

    <DIV ID="textDestination" style="border:3px coral solid; height:200px; width:340px;">

    <!-- Part 2: This line does print the image link.
    <script type="text/javascript">document.write('<a href="http://www.google.com" target="_BLANK"><img src="http://t2.gstatic.com/images?q=tbn:ANd9GcQV7hsgG998z8l7T5pyok6xqJFVqFmXlMAKmBFZQ0n06eLOEtojQQE1qwGjYQ" border=5></a>');</script>


    <SCRIPT LANGUAGE="JavaScript">
    Last edited by massalexx; 11-08-2011 at 04:23 PM.

  2. #2
    Join Date
    Jul 2008
    urbana, il
    script tags in innerHTML sets don't execute.

    you have to loops through the tags and either add them if the have a .src, or eval thier (s.text||s.textContent||s.innerText||s.innerHTML) if they don't...
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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