Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    TrainReq
    Guest

    document.write alternative

    I want to exicute the following code on the page using a .js file:
    Code:
    <a href=http://www.site.com>
    <img src="http://www.site.com/blah.gif" style="position:absolute; left:0px; top: 0px;" border="0"> </a>
    but everytime I use
    Code:
     document.write('<a href="http://www.site.com><img src="http://www.site.com/blah.gif" style="position:absolute; left:0px; top: 0px;" border="0"> </a>')
    It redirects/makes the page blank with just that code on it... I even tried putting a void on the code, and that didnt help either. Is there an alternative to document.write?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,603
    Thanks
    0
    Thanked 645 Times in 635 Posts
    The best way is to use the appropriate DOM commands to add the new content.

    The other (easier) way is to use document.innerHTML
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    There are a few ways you can do this. document.write is not one of them.
    1) Add this to the page
    Code:
    <a href="http://www.site.com" id="jslink" style="display:none;">
    <img src="http://www.site.com/blah.gif" style="position:absolute; left:0px; top: 0px;" border="0"> </a>
    Then in the javascript
    Code:
    <script type="text/javascript">
    window.onload = function()
    {
    document.getElementById('jslink').style.display = 'inline';
    }
    </script>
    2) Which may the be the route you are more likely to take as it doesn't require that you add to the html.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function addLoadEvent(func)
    {
    	var oldonload = window.onload;
    	if (typeof window.onload != 'function')
    	{
    		window.onload = func;
    	}
    	else
    	{
    		window.onload = function()
    		{
    			if (oldonload)
    			{
    				oldonload();
    			}
    			func();
    		}
    	}
    }
    
    addLoadEvent
    (
    	function()
    	{
    		var createLink = document.createElement('a');
    		createLink.setAttribute('href','http://www.site.com');
    		createLink.setAttribute('id','jslink');
    		var createImg = document.createElement('img');
    		createImg.setAttribute('src','http://www.site.com/blah.gif');
    		createImg.style.border = '0';
    		createLink.appendChild(createImg);
    		document.getElementsByTagName('body')[0].appendChild(createLink);
    		document.getElementById('jslink').style.position = 'absolute';
    		document.getElementById('jslink').style.top = '0';
    		document.getElementById('jslink').style.left = '0';
    	}
    )
    </script>
    </head>
    
    <body>
    </body>
    </html>
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

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