Search code examples
javascripthyperlinkhref

Using href to call a javascript function that creates a link


I'm trying to write something that will go to the URL that this function creates.

<script type="text/javascript"> 

function VLink() {
    var callNumHeading = $("th[class='BItemsHeader']:contains(NO.)");
    Num = $('table[id="B_items"] tbody tr td a').eq($(NumHeading).index()).text();
    var link = "http://someURL/?q="+Num;
    return link;
} 

$(document).ready(function() {
    var link = VLink();
});
</script>

I need to create code that will go to the link created in the function above. The closest I've come is this:

<a href="javascript:VLink(); ">My Link</a>

This code goes to a page that says it has the same URL as the original page, but all that appears on the page is the URL that I want to go to. The desired destination URL is appearing in the body of the page instead of the address bar.


Solution

  • window.location = VLink();
    

    That should redirect the page that is created by VLink().