最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to execute an anchorhref in my javascript? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to figure this one out but my mind has just gone blank.

  • I have a button element on my webpage with an id: e.g <button id="someId"></button>
  • In the document.ready function, I have an on click event which occurs when the user clicks the button. e.g. $('#someId').on('click', function() {
  • In this event there is an if statement which determines a value. Depending on the value, I want to execute a href in an anchor but I can't figure out the syntax.

The reason I am trying to execute the anchor in the javascript is because i'm passing a variable into the href. It could be true or false.

Here is what I have\what I'm trying to do. Any help would be great.

if (date1 <= date2) {
   //I want to execute this anchor
   <a href="@{Application.openthispage(true)}"></a>
}else{
   //otherwise execute this anchor
   <a href="@{Application.openthispage(false)}"></a>
}

I'm trying to figure this one out but my mind has just gone blank.

  • I have a button element on my webpage with an id: e.g <button id="someId"></button>
  • In the document.ready function, I have an on click event which occurs when the user clicks the button. e.g. $('#someId').on('click', function() {
  • In this event there is an if statement which determines a value. Depending on the value, I want to execute a href in an anchor but I can't figure out the syntax.

The reason I am trying to execute the anchor in the javascript is because i'm passing a variable into the href. It could be true or false.

Here is what I have\what I'm trying to do. Any help would be great.

if (date1 <= date2) {
   //I want to execute this anchor
   <a href="@{Application.openthispage(true)}"></a>
}else{
   //otherwise execute this anchor
   <a href="@{Application.openthispage(false)}"></a>
}
Share Improve this question asked Feb 1, 2013 at 19:42 EddieEddie 6214 gold badges13 silver badges23 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You're looking at the problem wrong. An anchor is a static HTML element that, when clicked, changes the current window location (i.e. URL). The JS equivalent looks something like this: window.location.href = 'http://www.google.';. So by setting window.location.href you will use JS to navigate to another URL.

The window.location method posted by Eli is the correct way to visit a link using JavaScript. However, if you can use a link instead of a button, you could just set the href of the link in the onclick. Here is a sample jsFiddle that visits a different url based on whether one input is greater than the other: http://jsfiddle/jw3Pd/

So when the user clicks the link, set the href to whatever link you want the user to visit.

$("#someId").on("click", function () { 
   if (date1 <= date2) {
      //I want to execute this anchor
      $("#someId").attr("href", "@{Application.openthispage(true)}");
   } else{
      //otherwise execute this anchor
      $("#someId").attr("href", "@{Application.openthispage(false)}");
   }
});
发布评论

评论列表(0)

  1. 暂无评论