开发者

How do I add a JavaScript result to a static HTML href attribute?

if I have a JavaScript method called getSomeStringValue(), is there a way to pull that value and use开发者_如何学编程 it as the href of a link, something like as follows?

(I'm aware the following code does not work.)

<a href="$:getSomeStringValue()" target="_blank">
    My Link
</a>


I think this would work:

<a href="#" target="_blank" id="mylink">
    My Link
</a>

<script>
document.getElementById("mylink").href = getSomeStringValue();
</script>


Just do this:

<a href="javascript:getSomeStringValue()" target="_blank">
    My Link
</a>

And in getSomeStringValue() you can do:

function getSomeStringValue(){
   //some code
   window.location = somewhere;
}


I used the idea posted by @Neal, and tweaked it a bit. This was the code I ended-up using if anyone's curious...

<a href="#" onclick="$:loadNewURL(parameter1, parameter2)">
    My Link
</a>

<script>
function loadNewURL(parameter1, parameter2) {
    var newURL = "http://";
    if (parameter1 == "Some Value")
        window.location = newURL + "/somepageA.aspx?detail=" + parameter2;
    else
        window.location = newURL + "/somepageB.aspx?info=" + parameter2;
}
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜