Skip to main content

javascript onclick combining 2 events does not work on ios (iPhone, iPad)



I'm combining 2 javascripts in the onclick to do 2 things:








1. Stop an audio player


2. Load and iframe that has a video





The following code works on all browsers but not iOS (iPhone, iPad)







<a class="aboutclick" href="#" onclick="stop2();document.getElementById('frame').innerHTML='&lt;iframe src=&quot;paul-on-paul.html&quot; width=&quot;100%&quot; height=&quot;600&quot; scrolling=&quot;no&quot; frameborder=&quot;no&quot;&gt;&lt;/iframe&gt;'"><img src="assets/video-cold.jpg" alt="paul-gregory-video" width="161" height="81" border="0" /></a>







The stop2() in the beginning of the onclick code is what turns off the music player. (it's Flash audio player so it doesn't apply to iOS). Tapping on the image does nothing on the iPad, iPhone with this code present.





If I remove stop2(); from the onclick code the iframe will load on iOS devices.





I need to be able to use the stop2() to kill the music but I also need it to work on the iOS devices.





Any suggestions?





Working example: http://www.fixxed.com/test/pg (click on the video thumbnail at lower right)


Comments

  1. First I would suggest you move your move all your onclick code into a function and then call that function on click, like this:

    window.foo = function(e) {
    stop2();
    document.getElementById('frame').innerHTML='<iframe src="paul-on-paul.html" width="100%" height="600%" scrolling="no" frameborder="no"></iframe>'
    }

    ...

    <a class="aboutclick" href="#" onclick="foo">
    <img src="assets/video-cold.jpg" alt="paul-gregory-video" width="161" height="81" border="0" />
    </a>


    That just makes things much nicer to read and more maintainable. Then I would suggest that you look into using mobile touch events instead of click events, you will end up with something like this:

    window.foo = function(e) {
    stop2();
    }

    window.bar = function(e) {
    document.getElementById('frame').innerHTML='<iframe src="paul-on-paul.html" width="100%" height="600%" scrolling="no" frameborder="no"></iframe>';
    }

    ...

    <a class="aboutclick" href="#" onclick="foo" ontouchstart='bar'>
    <img src="assets/video-cold.jpg" alt="paul-gregory-video" width="161" height="81" border="0" />
    </a>

    ReplyDelete
  2. It seems like iOS is choking on your stop2() call. You might want to try detecting if the browser is an iOS device (one simple and not-so-good way of doing that might be with the user-agent string) or (better) check to see if the browser supports that functionality using Modernizr. Then, if you detect that it's an iOS browser, just return from stop2().

    Hope this helps!

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex