页面重新加载后使 onclick 事件保持不变 [英] Make onclick event stick after page reload

查看:86
本文介绍了页面重新加载后使 onclick 事件保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Javascript 可以打开另一个窗口并为所有链接注册一个点击处理程序:

I have Javascript that opens another window and registers a click handler for all the links:

//Inside a class somewhere
this.file_browser_window = $(window.open("/filebrowser", "file_browser_window",
        "width=800,height=600"))

Event.observe(this.file_browser_window, 'load', function (){
     //This is the event I am after
     Event.observe(this.file_browser_window, 'click', handle_click_in_browser);
}.bindAsEventListener(this));

// The Handler function
function handle_click_in_browser(evt){
   evt.stop();  

   url = evt.target.href;

   if(url && url.endsWith('.png')){
       console.log("Image clicked");
       //REMMEMBER THIS URL ON MAIN PAGE
       this.close();
   }
   else{
       console.log("Regular stuff clicked", this);
       this.location = url; //<-- THIS is the breaking point
   }
}

但是,当用户单击该弹出窗口中的某个链接时,当页面重新加载时,我的 CLICK 处理程序就消失了!

However, when the user clicks on some link in that popup window, when the page reloads, my CLICK handlers are gone!

弹出窗口中的链接指向同一个域.

The links in the popup window point to the same domain.

现在,我无法更改弹出窗口中的源代码(html).我需要捕捉用户点击的链接标签的 href(如果它指向图片).

Now, I cannot alter the source(html) on the popup window. I need to catch the href of the link-tag (if it points to image) that the user clicked on.

如果有人感兴趣,我正在弹出窗口中运行 django-filebrowser.

I am running django-filebrowser in the popup window if anyone is interested.

推荐答案

您应该在弹出页面本身内为弹出页面应用单击事件处理程序.我认为没有任何理由让父窗口负责这些事件处理程序.

You should be applying your click event handlers for the popup page, within the popup page itself. I don't see any reason to make the parent window responsible for these event handlers.

这篇关于页面重新加载后使 onclick 事件保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆