views:

67

answers:

4

I've got this code, working only in firefox.

 <script type="text/javascript">
    function setVideo(url){
        url = url.replace("watch?v=","v/","i");
        var movie = document.getElementById('movie');
        movie.setAttribute('src',url+"&hl=en&fs=1&");
        var param = document.getElementById('paramm');
        param.setAttribute('value',url+"&hl=en&fs=1&");
    }
</script>

<object width="425" height="344">
            <param name="movie" id="paramm"></param>
            <param name="allowFullScreen" value="true"></param>
            <param name="allowscriptaccess" value="always"></param>
            <embed id="movie" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344" src=""></embed>
        </object>

<a href="#" onclick="setVideo('http://www.youtube.com/watch?v=3h1qQaRxY40')"&gt;example&lt;/a&gt;

PLEASE help, I have no idea, it's such a simple script, why is it not working?? thanks

+6  A: 

You are trying to dynamically change the video source in an embed element. This is notoriously tricky and in fact needs a workaround for browsers other than Firefox.

This question seems to provide a solution: Dynamically change embedded video src in IE/Chrome (works in Firefox)

Pekka
+3  A: 

You didn't provide any error message, so it's hard to say what needs to be fixed ...

One thing I did notice is that you use non-standard syntax for the "replace" method.

See https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String/replace for explanation and alternatives

ob1
…or look at the [ECMAScript specification of String.replace](http://ecma262-5.com/ELS5_Section_15.htm#Section_15.5.4.11), on which all JavaScript engine's are based.
Marcel Korpel
i'm sorry, in other browsers than firefox the video is blank .. there is no video
perfectDay
+2  A: 

You should probably use something like swfobject (http://code.google.com/p/swfobject) to embed the ENTIRE flash object when the setVideo function is called - don't just change the url. IE and chrome probably don't recognize the change.

David Radcliffe
+1  A: 

Don't use setAttribute with value. It is known to cause problems.

function setVideo(url){
    url = url.replace("watch?v=","v/";
    var movie = document.getElementById('movie');
    movie.src = url+"&hl=en&fs=1&";
    var param = document.getElementById('paramm');
    param.value = url+"&hl=en&fs=1&";
}
galambalazs