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

HTML anchor tag's onclick attribute does not call javascript function - Stack Overflow

programmeradmin2浏览0评论

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.

We're accessing our DotNetNuke module from "Server B", running Windows Server 2008 Standard, with Internet Explorer 9.

The issue happens when we click an anchor html element that has an onclick attribute, while accessing our module on Server B.

The anchor with the onclick is like:

<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

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.

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). 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.

What I've tried

I pared 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.

I pared the Server B Internet Explorer security settings to my security settings, and found no differences in setup.

On the advice of ments 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. It works on Server A and Dev, but doesn't do anything on Server B now. I think the core issue is that either the onclick attribute is not being recognized, or is being blocked somehow.

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). That is allowing the clicks to work, but I still haven't resolved why the onclick attribute is being ignored.

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.

If I edit the onclick handler in any way manually through the developer tools, like say removing the return false; from the onclick handler, it will work, but if I put the 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. 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.

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.

We're accessing our DotNetNuke module from "Server B", running Windows Server 2008 Standard, with Internet Explorer 9.

The issue happens when we click an anchor html element that has an onclick attribute, while accessing our module on Server B.

The anchor with the onclick is like:

<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

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.

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). 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.

What I've tried

I pared 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.

I pared the Server B Internet Explorer security settings to my security settings, and found no differences in setup.

On the advice of ments 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. It works on Server A and Dev, but doesn't do anything on Server B now. I think the core issue is that either the onclick attribute is not being recognized, or is being blocked somehow.

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). That is allowing the clicks to work, but I still haven't resolved why the onclick attribute is being ignored.

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.

If I edit the onclick handler in any way manually through the developer tools, like say removing the return false; from the onclick handler, it will work, but if I put the 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. 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.

Share Improve this question edited May 20, 2015 at 13:19 Zack asked May 18, 2015 at 17:14 ZackZack 2,8591 gold badge35 silver badges62 bronze badges 20
  • Does it have to be <a href ? Can it be instead <span onclick=... ? If you need to style it as a link - do so via something like style='color:blue;pointer:cursor' – Comfortably Numb Commented May 18, 2015 at 18:44
  • Are there any errors in the console when you load the page or click the link on server B? Also have you tried manually running OpenWindow() from the console on server B? It's possible that either OpenWindow() is not being defined (for example there could be an error above it that stops execution before the definition is run), or it is being defined more than once, and the version that is running is not the one you are expecting. – Matthew de Nobrega Commented May 18, 2015 at 19:18
  • @YuriyGalanter It doesn't have to be an anchor I suppose. Is there any reason you suspect that a span would allow the onclick to work instead of an anchor? – Zack Commented May 18, 2015 at 19:30
  • @MatthewdeNobrega The only error in the console is SEC7115: :visited and :link styles can only differ by color. Some styles were not applied to :visited which shows up on Server A, Server B, and Dev. Even with this error, the anchor still works on A and Dev, but not on B. – Zack Commented May 18, 2015 at 19:33
  • @MatthewdeNobrega I did run the OpenWindow function, by copy-pasting the onclick from the anchor into the console, and it works when I do that, and hits the breakpoints. – Zack Commented May 18, 2015 at 19:33
 |  Show 15 more ments

5 Answers 5

Reset to default 1

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.

Something, somewhere, is applying an onclick handler to all of your anchor tags.

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.

I would:

  • Save the entire web page from Site A
  • Save the entire web page from Site B
  • Run a diff tool against the two directories.

Have you tried to call window.open directly from the onclick attribute? 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).

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).

Check if you're showing JS errors in your Internet Options.

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. 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. target="_blank" is the best way that I know of to achieve this and enforcing browser behavior via 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:

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. 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.

发布评论

评论列表(0)

  1. 暂无评论