tags:

views:

50

answers:

2
<a href='http://www.domain.com' id='replace' style='text-decoration:none;color:black;font-size:10px;'>This is text link</a>

<script language="javascript">
var newURL = "mydomain.com/?refid=4877";
onload=function() {
    var dt = document.getElementById("replace");
    document.body.innerHTML = dt.getAttributeNode("href").value.replace(/domain.com/g,newURL);
}   

A: 

You should probably use JQuery:

$(document).ready(function(){
    $("#replace").attr("href","http://www.mydomain.com/?refid=4877");   
});
Jieren
-1, for telling the user he should use jQuery for such a simple task. If I had asked the question, I definitely wouldn't appreciate being told I "should probably" use jQuery just for replacing a simple string on a dom element's property. jQuery isn't needed for this task. It would also require extra time to learn jQuery if the asker has never used it before.
Andy E
Ah sorry about this. I'm still kind of unfamiliar with StackOverflow etiquette, but I guess when you have a damn good hammer, everything starts looking like a nail :-)
Jieren
+2  A: 

Just assign to the href attribute:

dt.href = dt.href.replace(/domain.com\/?/, newURL);

The optional trailing slash caters for browsers that automatically add a slash to hrefs.

harto
it work, but there some slash "/" at the end.http://www.domain.com/?refid=4877/
Agus Puryanto
Looks like a slash is appended to the original href by the browser. I'll tweak my answer
harto