开发者

Setting the href attribute replaces the text of the link

开发者 https://www.devze.com 2023-04-07 13:31 出处:网络
EDIT: I am not talking about the status bar display, I\'m talking about the text you click on to follow the link. /EDIT

EDIT: I am not talking about the status bar display, I'm talking about the text you click on to follow the link. /EDIT

I looked through a few similar posts and didn't find an answer. I have a link that I want displayed.

www.mylink.com

I want the href of the link to change every time the link is clicked.

href="www.mylink.com?R=340978"

The query string changes to a random number after each click to prevent the browser from caching the page. It's expected that the page will be changing frequently and be viewed frequently to assess those changes.

So I want people to see "www.mylink.com" but the link to lead to "www.mylink.com?R=34532" I thought I would achieve this with an onclick event that would apend a new random number and set the href attribute to the new link, however this is also changing the text that is displayed.

<script type="text/javascript">

var baseLink = "http://myDevLink/library/ea26098f-89d1-4305-9f75-afc7937f8336/25/test/036b602b-0bde-e011-957b-1cc1dee8bacd.html";
var link = "";

// Prime the link
$(document).ready(function() 
{
    NewRandomParam();
});

function NewRandomParam()
{
开发者_如何学Python    // Make sure one of the warning messages isn't displayed instead of the link        
    if (document.getElementById("link") != null) 
    {
        // Add a random number param to the query string to prevent the user from opening a cached version of the page
        link = baseLink + "?R=" + Math.floor(Math.random() * 1000000);
        document.getElementById("link").setAttribute("href", link);
    }
}

</script>

<body>

<div id="divContent" style="display:inline; border:0px" class="tab">
<pre style="margin-top: 0;" >
<font face="tahoma">
<a href="" id="link" onclick=NewRandomParam(); target="_blank">http://myDevLink/library/ea26098f-89d1-4305-9f75-afc7937f8336/25/test/036b602b-0bde-e011-957b-1cc1dee8bacd.html</a>
</font>
</pre>
</div>

</body>
</html>

As you can see from the above code, there is no query string on what should be displayed however when I view the page the value of the href is displayed with the query string. If I do a view source the correct url is there and should be displayed but it isn't.

Please enlighten me on where I'm being dumb.


The solution was a combination of things suggested here plus having to reset the links text to what I wanted it. Here is what worked.

<script type="text/javascript">

var baseLink = "<%= this.link %>";
var link = "";

$(document).ready(function () 
{
    // Add a timestamp param to the query string to prevent the user from opening a cached version of the page
    $('#link').mouseenter(function () 
    {
        var date = new Date();
        link = baseLink + "?Timestamp=" + date.getTime();
        $("#link").attr("href", link);
        $("#link").text(baseLink);
    });

});

</script>

Thanks to all of you.


Random numbers aren't a good idea here. There's always the chance of getting a duplicated number. Just use date.getTime() which should be "random" enough for your purposes.

Changing the href should NOT be changing the text in the link. They're sometimes the same text, but that's by putting the same information in two places (inside the href attribute) and as text inside the <a>linkname</a> tag itself. Changing one should never affect the other, unless you've got some other code that keeps things synced.

If you mean you want the change what's displayed in the window status bar when the link's hovered, good luck. most browsers have mechanisms to prevent such fiddling in the first place, as changing the status bar text was heavily abused by spammers and scammers.


<script type="text/javascript">
   $(document).ready(function() {

      var baseLink = "http://myDevLink/library/ea26098f-89d1-4305-9f75-afc7937f8336/25/test/036b602b-0bde-e011-957b-1cc1dee8bacd.html";
      var link = "";

      $('#link').mouseenter(function() {
         link = baseLink + "?R=" + Math.floor(Math.random() * 1000000);
         $("#link").attr("href", link);
      });
   });
</script>
0

精彩评论

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

关注公众号