How to store URL of clicked hyperlink

那年仲夏 提交于 2019-12-25 03:57:09

问题


I am displaying a warning dialog box whenever user tries to navigate from current page without saving data on current page. Its working fine now I want to call a function (Spring controller, its kind of java function which handled URL mappings ) when user clicks on Ok (in warning dialog box) and then he should get redirectd to desired page.
Let me try to make it simple (Its confusing for me also)
1) User is on registration page, he/she made some changes and didn't save it.
2) Now user clicked on any other link for example he clicked on About Us link.
3) Now I want to execute my spring controller.
4) After execution of controller user should get navigated to About Us page.
For this I need to save value of clicked hyperlink and pass it to my spring controller.
So how can I store URL of clicked link (URL of About Us page in above example) ?

PS: I will really appreciate if anybody can edit my question to make it easier to understand.


回答1:


if you use jQuery, you can attach an event handler to onclick to all links in the page and once clicked the handler should save the href attribute to some variable. then create a onbeforeunload event listener on your window, where you can use the value however you want, call your controller or save the value in a cookie or something.




回答2:


Are all the links on the page pointing to your spring application? If there are no external links anywhere (pointing to external resource) - then you could write a simple Filter where you can save the requested page into the session.

Otherwise, if there are links to external resources - you would need to rewrite them from www.external.com to www.my.com\MagicController?requestedPage=www.external.com. Controller will save the link and send a redirect in HTTP header to the requested page. This is a common practice - even google does that (check out the google search result links for how it will look like).

Added: Weird, but google does that only on some rare occasions, so you probably won't be able to find an example there.




回答3:


Don't require to preserve the href of selected tab.Do one thing attach same javascript function with each tab and pass the "this" as parameter of function.

Function of the javascript is

function Attach(ele)  
{

// 1. Find the handle of selected tag and store in the variable. 
   ele=$(ele);

// 2. Find the value of href
   var href=ele.attr("href");

// 3. Perform server side operation you want.

// 4. redirect to another page.
   window.location=href;

  return false;

}


来源:https://stackoverflow.com/questions/6676704/how-to-store-url-of-clicked-hyperlink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!