简体   繁体   中英

Can we set attributes of HTML5 tags like target or src using javascript?

I am new to this forum. I am facing a small problem in passing "target" attribute into embed vlc TAG. Please look into the below code and help me out..

I tried to change the "width" attribute and it is being change successfully. I don't understand why there is a problem with "target" only. But it works if we just add the target directly in the embed tag.

Please help me out.

My aim is to be able to pass different file paths so as to view the video I wanted. I tried using video tag and change its src attribute but that,s not working too. Here is the code

<html>
<head>
<script language="javascript">

window.onload = function() 
{
document.getElementById("player").width = "800";
//document.getElementById("player").target = "sample.mp4";
document.getElementById("player").setAttribute("target","sample.mp4"); // only this attribute is not being passed
alert(document.getElementById("player").getAttribute("target")); // I am also getting the alert as 'sample.mp4'
}
</script>
</head>

<body>
<embed type="application/x-vlc-plugin" id="player" name="VLC" autoplay="yes" loop="no" volume="100" width="640" height="480" />
</body>
</html>

I had a similar issue and in the end i decided to use the VLC playlist object instead of setting the target attribute.

var vlc = document.getElementById("player");
vlc.playlist.items.clear();
var id = vlc.playlist.add(src);
vlc.playlist.playItem(id);

The full documentation for the VLC playlist can be found here .

The HTML5 embed element doesn't have a "target" attribute according to w3schools.com: http://www.w3schools.com/html5/tag_embed.asp ).

However, the VLC plugin apparently reads it: http://wiki.videolan.org/Documentation:WebPlugin#Embed_tag_attributes

That seems strange to me; if w3schools is correct, the target attribute would fail to validate?

It sounds to me like you're wanting to change the attribute which specifies the playing video, in order to change it, yes? I can't say I'm an expert, as I've not dealt directly with teh VLC plugin like this, but I wonder if this might help: http://forum.videolan.org/viewtopic.php?f=16&t=57845

Also, from http://wiki.videolan.org/Documentation:WebPlugin#Playlist_object , it seems that accessing the plugin's playlist object, adding a new item, and then immediately using next() could achieve what you want, in a VLC-native way.

Why not use jQuery? then it's as easy as:

$('#player').attr('target', 'sample.mp4');

Retreiving attribute values is as easy as:

$('#player').attr('target');

for further reading, use the jQuery documentation:

http://docs.jquery.com/Main_Page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM