I have a JQuery script that hides a div on my web page until a button is clicked. It then eases in a window to display the content of the hidden div. If I put a static image in the div it works fine. But if I put a piece of JavaScript in the div to execute a video script it does not work. It appears that the JQuery script will not execute the JavaScript for some reason. But if I remove the JQuery Script and allow the div to be displayed when the page is loaded it again works fine.
Does anyone know why this would happen and offer a solution. I am happy to disclose the JQuery Script, as well as the CSS and HTML code if you need to see it. I am just not sure if the rules of this site actually permit doing that.
Definitely do that. A live link is best, especially in a case like this. If you don’t want to expose your URL, you could set up a demo at a site like jsFiddle or CodePen.
Thanks Ralph - I have a testing site that I am using to develop this code. It is located here: http://rgstest.com/pjg/
I have put everything back together so the JQuery script has the code for displaying the video and the page has the div with the JavaScript code. As you will see when the button for opening the video is clicked - nothing happens.
You don’t actually have the jQuery library on that page, although you do have jQuery tools loaded. You need to have a link to the jQuery library on the page, and it needs to go above any other jQuery scripts. You could just add a link to the online version if you like. E.g.
As you can see, Ralph, the reason you’re not seeing jQuery, is that the file is dynamically writing it to the page.
It also writes several minified JavaScripts to the page, which are rather impenetrable.
To approach this from another way: it seems to me that you have a flash movie (http://rgstest.com/pjg/videos/eflv000_float_bar_dark_cft.swf) and that when someone clicks a button, you want this movie to appear in a modal overlay (lightbox effect).
Hi Pullo - Perhaps I need to explain a bit about what is going on. I originally had a Flash (.swf) video in this location and it worked just fine with the JQuery fade in and out. But what I wanted to to do was replace the Flash with a video that could be viewed in multiple user agents including the iPhone/iPad etc. So what is happening here is that the JavaScript in the div is loading another piece of JavaScript that makes a determination as to what type of user agent is loading the web page and initializing the proper video for that user agent. ie mp4, swf, etc. This code is actually being generated by a commercial product called EasyFLV, so additional code is also being generated to format the look of the video, such as the opening and closing image, the color scheme etc. I am sure that the root cause of this problem has to do with the multiple levels of JavaScript that is being used here. BUT - if I remove the JQuery portion of the code and allow the div to be displayed upon loading of the page, the video works fine. It is only when I surround the div with the JQuery that the button does not work.
Does this make better sense of the code for you guys?
It seems to be that when you put that script anywhere in the page apart from in the <head> section of the document it just throws all kinds of nasty errors.
As I said, the JS it is generating is impenetrable and it is hard to tell where the exact error is coming from.
Plus, it seems that document.write doesn’t work in XHTML - http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite
If I was you I would use the <video> tag and let the browser decide what it wants to play and what it doesn’t.
If you could give me links to the same swf file in mp4, ogg and webm format, I can make you a demo that will play in all browsers.
So, here’s a demo using the [URL=“http://www.videojs.com/”]video.js plugin, as well as the mp4 version and the webm version of your film. Nothing more!
That’s enough for it to be playable on mobile devices, as well as older and modern browsers (I tested on the iPhone, iPad, latest Chrome, Opera and Firefox, as well as IE 7 - 10).
Hi Pullo - Perhaps you can explain this to me. I have been able to successfully incorporate your sample code into my actual testing page. I put the JavaScript code:
if I link this file before the <head> tag along with all my other scripts, it does not work. But if I link this script at the end of my content it works just fine. It seems pretty picky as to where I can put it. It won’t work immediately following the <body> tag either. I have to complete all of the initial PHP includes at the beginning of the page before it seems to work. I’m sure there is an explanation for this, but I just do not know what it is.
If you want to put the JS in an external file and include it in the <head> section of your document, then you need to wrap everything in a $(document).ready() callback, as otherwise you are trying to reference elements in the DOM before they exist.
Saying that, it is good practice to put all of your JavaScripts just before the closing <body> tag, in which case $(document).ready() becomes obsolete.
The reason that this is a good idea is because when a browser loads a script, it does not spawn a new thread to do so, rather it waits until it has loaded the script before it proceeds to the next line.
Putting scripts just before the closing <body> tag therefore makes for a snappier, more responsive experience for your users.