Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE href="javascript:customFunction()" not firing on first frame load

I have a custom date picker popup that isn't working in IE sometimes. It works in Chrome and Edge fine.

The code looks something like this:

<frameset>
    <frame>Buttons for next/prev month/year</frame>
    <frame>This is the actual calendar that gets redrawn when the above buttons are used
        <a href="javascript:parent.opener.setDate(1);">1</a> //there's a different anchor tag for each day of the month
    </frame>
<frameset>

So here's where it gets kind of weird. We have two networks, call them old and new. Old has probably a lot of undocumented global policy changes and new is probably close to the gov standard. This works on any browser on the old network, but not IE (11) on the new network. It works in Edge though. Additionally, if the top frame buttons are used to pick the next/prev month, or just the "Today" button, then all of the bottom frame anchor links work normally. There are no console errors/warnings, nothing in the network monitor showing a request returned an error code, the clicks just don't register. I put a breakpoint inside customFunction() and it won't break when the links don't work, but it will break if the link will work.

The only other thing that seems odd to me is that the code for the whole popup looks something like:

str = "<frameset><frame name='topFrame' " + 
    "src='javascript:parent.opener.drawTop'></frame><frame name='bottomFrame' "+
    "src='javascript:parent.opener.drawBottom'><frame</frameset>"

document.write(str);

I did look to check and the code that redraws the bottom frame when the prev/next/etc buttons are used is the same function that gets called during the first load.

However, what seems odd about this is that on the first load the DOM inspector shows everything (top frame, bottom frame including all the individual numbers for each day of the month, etc), but the Debugger (F12 tools) doesn't show the code loaded with the document.write(str); line. To be able to see that code and set break points I have to use the prev/next buttons and then an additional .html file shows up in Debugger which has the constructed HTML that matches the DOM.

like image 266
tenmiles Avatar asked Mar 22 '17 16:03

tenmiles


People also ask

Can we call JavaScript function in href?

In JavaScript, you can call a function or snippet of JavaScript code through the HREF tag of a link. This can be useful because it means that the given JavaScript code is going to automatically run for someone clicking on the link. HREF refers to the “HREF” attribute within an A LINK tag (hyperlink in HTML).

Does Onclick work on link?

This is a type of JavaScript link - the onclick attribute defines a JavaScript action when the 'onclick' event for the link is triggered (i.e. when a user clicks the link) - and there is a URL present itself in the onclick attribute.

Can we pass function in href?

The anwer is: not possible.

How do you call Onclick in a tag function?

You could write <a href="javascript:window. onclick = location. reload;">Activate me to reload when anything is clicked</a> . Within HTML, onclick can mean something on its own, as long as its part of an HTML tag: <a href="#" onclick="location.


2 Answers

try this:

1)

<a href="javascript:parent.opener.setDate(1); void(0);">1</a>

2)

<a href="javascript:function(){parent.opener.setDate(1); return false;}">1</a>

3)

<a href="#" onclick="javascript:parent.opener.setDate(1); return false;">1</a>

4) check your code. Maybe your frame has attribute 'sandbox'. This attribute can block javascript. Example:

 <iframe src="URL" sandbox>
like image 152
Nutscracker Avatar answered Oct 11 '22 14:10

Nutscracker


Aside from the great suggestions by Nutscracker, I've also had my share of vague problems with document.write and event handlers not attaching in IE. The problem commented on by ConnorsFan could be the cause here, but you could also try:

document.body.innerHTML = '<frameset><frame name="topFrame" ' + 
'src="javascript:parent.opener.drawTop"></frame><frame name="bottomFrame" '+
'src="javascript:parent.opener.drawBottom"><frame></frameset>'

You might also want to check this code is actually being called, maybe the real working popup is loaded from somewhere else by the prev/next buttons and this is just some leftover stuff.

like image 39
Fasermaler Avatar answered Oct 11 '22 13:10

Fasermaler