简体   繁体   English

HTML锚标记的onclick属性不会调用javascript函数

[英]HTML anchor tag's onclick attribute does not call javascript function

We have a DotNetNuke module running in an instance of DotNetNuke 5.4.4, installed on "Server A", a Windows Server 2008 R2 Standard machine with IIS 6.1 and Internet Explorer 11. 我们有一个DotNetNuke模块在DotNetNuke 5.4.4的实例中运行,安装在“Server A”上,这是一台带有IIS 6.1和Internet Explorer 11的Windows Server 2008 R2 Standard机器。

We're accessing our DotNetNuke module from "Server B", running Windows Server 2008 Standard, with Internet Explorer 9. 我们使用Internet Explorer 9从运行Windows Server 2008 Standard的“Server B”访问我们的DotNetNuke模块。

The issue happens when we click an anchor html element that has an onclick attribute, while accessing our module on Server B. 当我们在服务器B上访问我们的模块时,单击具有onclick属性的anchor html元素时会发生此问题。

The anchor with the onclick is like: onclick的锚点如下:

<a onclick='OpenWindow("/DotNetNuke/DesktopModules/Module/View.aspx?dt=%c2%b2%c2");return false;' 
   href="http://000.00.0.0/DotNetNuke/DesktopModules/Module/View.aspx?dt=%c2%b2%c2"
   target='_blank'
   jQuery1431968126278="42">Doc name (SSN-SS-SSNN)</a>

And the OpenWindow function is like this 而OpenWindow函数就是这样的

function OpenWindow(url) {
  window.open(url, '', 'top=15,scrollbars=yes,menubar=no,height=800,width=800,resizable=yes,toolbar=no,location=no,status=no');
}

As you can see we have an anchor element, with an onclick attribute, where it is supposed to call the OpenWindow javascript function, then return false, so the default action for clicking an anchor (browse to the href) does not happen. 正如您所看到的,我们有一个带有onclick属性的anchor元素,它应该调用OpenWindow javascript函数,然后返回false,因此单击一个锚点(浏览到href)的默认操作不会发生。

When we click this link though ( ONLY on server B ), we get no popup window, no breakpoints in the OpenWindow function are hit, and the browser navigates to the href by opening a new tab (View.aspx). 当我们单击此链接时( 仅在服务器B上 ),我们没有弹出窗口,OpenWindow函数中没有断点,浏览器通过打开一个新选项卡(View.aspx)导航到href。 This suggests to me that the OpenWindow function referenced by the onclick attribute is not even running for some reason, even though it is on the anchor element, and works on any other server. 这告诉我onclick属性引用的OpenWindow函数由于某种原因甚至不运行,即使它在anchor元素上,并且可以在任何其他服务器上运行。

What I've tried 我试过的

I compared the security settings, web.config files, and DotNetNuke settings between the Server A DotNetNuke and my local developer DotNetNuke instances, and found no differences in setup. 我比较了服务器A DotNetNuke和我的本地开发人员DotNetNuke实例之间的安全设置,web.config文件和DotNetNuke设置,发现设置没有差异。

I compared the Server B Internet Explorer security settings to my security settings, and found no differences in setup. 我将Server B Internet Explorer安全设置与我的安全设置进行了比较,发现设置没有差异。

On the advice of comments on this question, I tried changing the anchor tag to a span tag instead (removed href and target attributes), and I am seeing the same behavior. 根据对此问题的评论建议,我尝试将锚标记更改为span标记(删除了href和目标属性),我看到了相同的行为。 It works on Server A and Dev, but doesn't do anything on Server B now. 它适用于服务器A和开发人员,但现在不在服务器B上执行任何操作。 I think the core issue is that either the onclick attribute is not being recognized, or is being blocked somehow. 我认为核心问题是onclick属性未被识别,或者被某种方式阻止。

I've now gone further and changed most of the <a>...</a> tags into <span>...</span> tags, with specific classes, that I then attach jQuery(...).live('click', ...) handlers to (using jQuery 1.4.2). 我现在更进一步,将大部分<a>...</a>标签更改为<span>...</span>标签,使用特定的类,然后我附加jQuery(...).live('click', ...)处理程序(使用jQuery 1.4.2)。 That is allowing the clicks to work, but I still haven't resolved why the onclick attribute is being ignored. 这是允许点击工作,但我仍然没有解决为什么忽略onclick属性。

What's weird.. 有什么奇怪的..

If I open the developer tools (IE9), then click the "Edit" button to turn edit mode on and off again, all of the onclick attributes on anchor tags and img tags start working correctly, until I reload the page. 如果我打开开发人员工具(IE9),然后单击“编辑”按钮再次打开和关闭编辑模式,锚标签和img标签上的所有onclick属性开始正常工作,直到我重新加载页面。

If I edit the onclick handler in any way manually through the developer tools, like say removing the return false; 如果我通过开发人员工具以任何方式手动编辑onclick处理程序,比如说删除return false; from the onclick handler, it will work, but if I put the return false; 从onclick处理程序,它将工作,但如果我把return false; back to make it like I never changed anything, it stops working again. 回来让它变得像我从未改变任何东西,它再次停止工作。

What I'm trying to figure out 我想弄清楚的是什么

I am hitting a wall with what to check to figure out this issue. 我正在找一块墙来检查这个问题。 I can't reproduce it on my developer machine, and it works on Server A as well, so the code is working perfectly fine. 我无法在我的开发人员计算机上重现它,它也适用于服务器A,所以代码工作得非常好。 I'm thinking there must be a setting that I am overlooking somewhere, but where? 我想我必须有一个设置,我忽略了某个地方,但在哪里? I have no idea what else to check at this point, and I'm looking for ideas. 我不知道此时还有什么要检查,我正在寻找想法。

I don't have an exact answer, but I can give you the general idea of what is happening and why. 我没有确切的答案,但我可以给你一般的想法,了解发生了什么以及为什么。

The two servers are not getting identical content -- perhaps they are pointed at different CDNs, perhaps one has an old file of a JavaScript file somewhere -- you'll have to walk through them one by one. 这两个服务器没有得到相同的内容 - 也许它们指向不同的CDN,也许有一个旧文件的JavaScript文件 - 你必须逐个浏览它们。

Something, somewhere, is applying an onclick handler to all of your anchor tags. 某个地方的东西正在为所有锚标签应用onclick处理程序。

At first I thought it was something attached to the body element and targeting your links, but I ruled that out. 起初我以为它是贴在身体元素上的东西,并以你的链接为目标,但我已经排除了这一点。 The fact you can edit the HTML and save it and then the link works means you are detaching whatever was attached to that link element. 您可以编辑HTML并保存它然后链接工作的事实意味着您正在分离附加到该链接元素的任何内容。

I would: 我会:

  • Save the entire web page from Site A 从站点A保存整个网页
  • Save the entire web page from Site B 从站点B保存整个网页
  • Run a diff tool against the two directories. 对两个目录运行diff工具。

Have you tried to call window.open directly from the onclick attribute? 您是否尝试直接从onclick属性调用window.open It could be that the function is not loaded at the time the link is clicked, so it produce an error, so return false is not called, and the link proceed with its default behaviour (in this case open a tab). 可能是在单击链接时未加载该函数,因此它会产生错误,因此不会调用return false ,并且链接将继续其默认行为(在本例中为打开选项卡)。

If this is the case, look at the loading order of that function, or try placing it in a different part of your code (maybe within the onload event). 如果是这种情况,请查看该函数的加载顺序,或尝试将其放在代码的不同部分(可能在onload事件中)。

Check if you're showing JS errors in your Internet Options. 检查您是否在Internet选项中显示JS错误。

It might be a bit childish, but since you are opening a window as a Pop Up there can be issue that the URL from the site might be blocked from the browser. 它可能有点幼稚,但由于您打开一个窗口作为弹出窗口,可能会有问题,该站点的URL可能会被阻止从浏览器。 It should work at all times if you just replace 如果你只是更换,它应该始终工作

window.open(url, '', 'top=15,scrollbars=yes,menubar=no,height=800,width=800,resizable=yes,toolbar=no,location=no,status=no');

with

window.open(url);

Have you tried modifying your tag to: 您是否尝试将标记修改为:

<a href='javascript:MyFunction(this);' data-url='YourUrl'>Name</a>

MyFunction:  function(obj) {

var element = $(obj);
window.open(element.data("YourUrl"));
}

Note that if the user's browser is configured to open Windows as tabs, there is not a workaround. 请注意,如果用户的浏览器配置为以选项卡形式打开Windows,则无法解决方法。 target="_blank" is the best way that I know of to achieve this and enforcing browser behavior via GP. target =“_ blank”是我所知道的实现此目的并通过GP强制执行浏览器行为的最佳方式。

Sounds like it's something not in the scope of the code you have provided. 听起来它不属于您提供的代码范围。 You mentioned opening the IE9 dev-tools makes it work, I suggest try adding console -polyfill, something like: 你提到打开IE9 dev-tools使它工作,我建议尝试添加console -polyfill,类似于:

if(typeof(console)=="undefined") {
  var console = {
    log : function() {},
    error : function() {},
    debug : function() {}
  };
}

Just in case some of your code is outputting things into the console and the JS code stops working after that event. 以防你的一些代码将内容输出到控制台,JS代码在该事件之后停止工作。 Could be some external library, which after loading starts outputting things to the console - that might work differently on different servers because of caching settings and/or network latency. 可能是一些外部库,在加载后开始向控制台输出内容 - 由于缓存设置和/或网络延迟,可能在不同服务器上的工作方式不同。

NOTE : to test this properly, remember to add this into a SCRIPT tag before you load any JS code or import any external library. 注意 :要正确测试,请记住在加载任何JS代码或导入任何外部库之前将其添加到SCRIPT标记中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM