JavaScript Tutorial/HTML Tags/setAttribute

Материал из Web эксперт
Перейти к: навигация, поиск

Change hyperlink innerHTML by calling the setAttribute() function

<html>
<head>
<script language="JavaScript" type = "text/javascript">
<!--
function ChangeLink() {
    var newlink = document.getElementById("urltext");
    var curlink = document.getElementById("oldurl");
    curlink.href = ("http://" + newlink.value);
    curlink.innerHTML = newlink.value;
}
function NewWindow(prefval) {
    var curlink = document.getElementById("oldurl");
    if (prefval == 1)
    {
        curlink.setAttribute("target", "_blank");
    }
    else if (prefval == 2)
    {
        curlink.removeAttribute("target");
    }
}
//-->
</script>
</head>
<body>
<form>
<a id="oldurl" href="http://www.wbex.ru">www.wbex.ru</a>
<P><input type="radio" value="V1" name="R1" onClick="NewWindow(1)">Open in New Window</p>
<P><input type="radio" name="R1" checked value="V2" onClick="NewWindow(2)">Open in Same Window</p>
   <input type="text" id="urltext" size="20">&nbsp;
   <input type="button" value="Change Link" name="B3" onClick="ChangeLink()"></p>
</form>
</body>
</html>