0

It may be a silly question but I really stuck there for long time. Hope someone can give a hand and explain the root cause here. Thanks.

In my processing JSP, I have following code to call a Javascript function. All values are got from front-end JSP through POST action:

<a onclick="loadPredecessorPage(${param.predecessorName})" href="javascript:;">
   ${param.predecessorTitle}
</a>

At the same time, in its above header, I have this following javascript function:

<script language="javascript">
      function loadPredecessorPage(predecessorName)
      {
           var url = predecessorName +".jsp";
           document.location.href = url;
      }
</script>

I have used firebug to observe the values. They were correct in the sense of values. Actually, the predecessorName variable is just the name of model class in my own MVC design. If the program was working, the user should be able to click the link and access corresponding front-end view JSP of the specific model class.

Can someone point out the faults I made in above code?

ShadowScorpion
  • 207
  • 6
  • 21

1 Answers1

0

If you only want to open a static URL, you could simply pass the complete URL to your link's href via JSP:

<a href="${param.predecessorName}.jsp">
   ${param.predecessorTitle}
</a>

If nonetheless you would like to open the page in a JavaScript function, continue reading.
You should use window.location instead of document.location (see here). If you want to pass your URL in HTML, you should do this inside a corresponding data- attribute. Also, it is recommended that you add your listeners via JavaScript at runtime.

Example HTML:

<a id="loadPage" href="#" data-url="http://stackoverflow.com">Load Page</a>

jQuery Code:

$('#loadPage').on('click',function() {
    var url = $(this).data('url');
    window.location.href = url;
});
Community
  • 1
  • 1
MCL
  • 3,985
  • 3
  • 27
  • 39
  • Thank you for your suggestions, MCL. I have tried to use window.location instead of documen.location as you mentioned above. It seemed not working as well. Instead of that, I realized that actually I no need a javascript or Jquery to do this dynamic redirection. Hence, I just chose to achieve it by using static URL. Thanks again. – ShadowScorpion Feb 06 '13 at 01:17
  • @ShadowScorpion If the first variant provided a solution for you, you may consider accepting it as an answer. – MCL Feb 06 '13 at 09:19