Results 1 to 6 of 6

Thread: Inserting Hyperlink Inside A Javascript?

  1. #1
    Join Date
    Sep 2008
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Inserting Hyperlink Inside A Javascript?

    I have two part of this script head and body sections. I want to insert hyperlink on text "Prices correct as at:" section so when clicked I take user to another page. I searched thru the web and internal search of the forum but can't find the solution.


    <head>
    <script type="text/javascript">

    function getLastMod() {
    var myFrm = document.getElementById('myIframe');
    var lm = new Date(myFrm.contentWindow.document.lastModified);
    var dt = lm.getDate();
    var mm = lm.getMonth() + 1;
    var yy = lm.getFullYear();
    var lmDate = mm + "-" + dt + "-" + yy;
    document.getElementById('LastModified').innerHTML = "Prices correct as at: " + lmDate;
    }

    </script>
    </head>



    <body>
    <span id="LastModified"></span>

    <iframe id="myIframe" onload="getLastMod()" src="prices.txt" style="display:none;">
    </iframe>

    </body>
    </html>

  2. #2
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    here is an example:

    Code:
    <a href="http://www.google.com">Google</a>
    -magicyte

  3. #3
    Join Date
    Sep 2008
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    I tried both example below and It works in ie but not work for ff and opera?

    document.getElementById('LastModified').innerHTML = '<a href="destination.html">Prices correct as at: </a>' + lmDate;

    document.getElementById('LastModified').innerHTML = '<a href="destination.html">Prices correct as at: ' + lmDate + '</a>';

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Check the following code
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <title>Untitled Document</title>
            <style type="text/css">
            </style>
            <script type="text/javascript">
                function getLastMod(){
    		var myFrm = window.frames['myiframe'];
    		var lm = new Date(myFrm.document.lastModified);
                    var dt = lm.getDate();
                    var mm = lm.getMonth() + 1;
                    var yy = lm.getFullYear();
                    var lmDate = mm + "-" + dt + "-" + yy;
    
    				//Based on the innerHTML
                    document.getElementById('LastModified').innerHTML = "<a href='your_destination_page.htm'>Prices correct as at: " + lmDate; + "</a>";
    				
    				//DOM based method
    				/*var aEl = document.createElement('a');
    				aEl.href = 'your_destination_page.htm';
    				aEl.appendChild(document.createTextNode('Prices correct as at: ' + lmDate));
    				document.getElementById('LastModified').appendChild(aEl);*/
                }
            </script>
        </head>
        <body>
            <span id="LastModified"></span>
            <iframe id="myIframe" name="myiframe" onload="getLastMod();" src="prices.txt" style="display:none;">
            </iframe>
        </body>
    </html>
    In the above mentioned code I have two method for doing the same job (inserting the hyperlinks):

    1. Using innerHTML
    2. Using DOM

    The above code comments the DOM based method. if you want you can uncomment the code that comes under the DOM method. Make sure to comment the innerHTML based code otherwise you'll get two links in the span element. Also make sure that you do mention a correct destination page in anchor element..

    I've highlighted all the portion, which I've changed.

    Works well in IE and FF. Though I don't know what exactly you are trying to do.
    Hope this helps.

  5. The Following User Says Thank You to codeexploiter For This Useful Post:

    nizamo (09-05-2008)

  6. #5
    Join Date
    Sep 2008
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    This code is for showing last updated date via getting another file's timestamp. I just want to open updates page when visitor clicks on "Prices correct as at:" text but in ff text is not clickable. I tried your both code (I think uncommenting dom should be removing /* and */ if I understand right) I also added these marks to inner html code but this seemed didn't work.

    You can see it at www.aymavisi.org bottom right corner of the blue moon.

  7. #6
    Join Date
    Sep 2008
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    Hold on. I just removed text on script "Prices correct as at:" and add it as seperate text then put hyperlink and solved the problem. Sorry guys but I'm just nobo. Sorry again and thanks lot for trying to helping me.

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
  •