最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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>

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>
Share Improve this question edited May 10, 2011 at 18:57 Paul D. Waite 99k57 gold badges203 silver badges271 bronze badges asked May 10, 2011 at 18:52 WEFXWEFX 8,5728 gold badges69 silver badges104 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

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>
Post a comment

comment list (0)

  1. No comments so far