homepage Welcome to WebmasterWorld Guest from 54.237.54.83
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Browsers / Opera Browser Usage and Support
Forum Library, Charter, Moderator: open

Opera Browser Usage and Support Forum

    
Opera Asynchronicity
How Can I Deal with an Opera JS Function That is Asynchronous?
cmarshall




msg:3502275
 7:54 pm on Nov 11, 2007 (gmt 0)

I had posted this question in the JavaScript and Ajax Forum [webmasterworld.com], but there are crickets chirping. Maybe you guys can help.

Basically, Opera treats a JavaScript call as an asynchronous call, while other browsers don't. I'd like to know if there is a way to either force the call to be synchronous, or a better way to work around it than the rather kludgy fashion demonstrated here.

---

A friend of mine asked me to post this question on this forum, and see what suggestions come about. This sort of dovetails into an earlier discussion I remember about JavaScript knowing when a page is being rendered for printing (We figured out that there was no way to do this).

He figured a way to work around it, but it seems quite a hack.

Here goes:

The CSS:

<style type="text/css">

div#div1 {
border: 2px solid red;
}

div#div2 {
border: 2px solid green;
}

</style>

The JavaScript:

<script type="text/javascript">

function print_test() {
document.getElementById('div1').style.display = 'none';

window.print();

document.getElementById('div1').style.display = '';

return false;
}

</script>

The XHTML:

<div id="div1">This &lt;div&gt; shouldn't print.</div>
<br />
<br />
<div id="div2">This &lt;div&gt; should print.</div>
<br />
<br />
<a href="#" onclick="return print_test()">Print via Javascript</a>

The problem is that, in Opera, the line immediately after the window.print(); line is executed immediately, so the hidden <div></div> is shown in the printout.

Basically, Opera treats window.print(); as an asynchronous operation, while other browsers don't.

His kludgy fix is thus:

<script type="text/javascript">

function print_test() {
document.getElementById('div1').style.display = 'none';

window.print();

if (window.opera){
setTimeout("document.getElementById('div1').style.display = ''", 1);
}
else {
document.getElementById('div1').style.display = '';
}

return false;
}

</script>

Any ideas?

 

tedster




msg:3514235
 6:17 am on Nov 27, 2007 (gmt 0)

Well, the crickets are pretty loud in here, too, aren't they?

I'm no javascript guru, but I have done some research about this issue. Does this page from dev.opera.com offer any help? In particular, this quote caught my eye:

Nested Events

There is a special case where events are not sequenced, but nested. If an event is fired explicitly through script using the dispatchEvent()-method (fireEvent() in Internet Explorer), the event is dispatched immediately. The initial script will only continue when the dispatched event has finished (and the default action has executed).

[dev.opera.com...]


cmarshall




msg:3514715
 8:39 pm on Nov 27, 2007 (gmt 0)

Thanks, Jimminy!

I'll pass that on to my friend, and see if this addresses his issue. He's not very happy with his kludgy workaround.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Browsers / Opera Browser Usage and Support
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved