admin管理员组

文章数量:1026289

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>

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>

本文标签: How do I add a JavaScript result to a static HTML href attributeStack Overflow