PDA

View Full Version : Inserting Hyperlink Inside A Javascript?



nizamo
09-04-2008, 06:54 PM
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>

magicyte
09-04-2008, 10:19 PM
here is an example:


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

-magicyte

nizamo
09-05-2008, 11:39 AM
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>';

codeexploiter
09-05-2008, 12:08 PM
Check the following 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.

nizamo
09-05-2008, 01:31 PM
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.

nizamo
09-05-2008, 01:44 PM
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.