/    Sign up×
Community /Pin to ProfileBookmark

How do I insert a var value into an SRC?

Hi guys,

Complete novice at jarascript here, I used to do a fair bit of php but never really touched javascript. Im just trying to bodge something together using code I have found on the internet to play movies in a browser.

I am using the following code to get a movie file name from the URL (eg – [url]www.mywebsite.com/movies.html?file=filename):[/url]

[CODE]<script>
function gup( name )
{
name = name.replace(/[[]/,”\[“).replace(/[]]/,”\]”);
var regexS = “[\?&]”+name+”=([^&#]*)”;
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return “”;
else
return results[1];
}

var file = gup(‘file’);
</script>[/CODE]

How do I then insert the variable into the following code to get the correct movie to display?

[code=html]
<video class=”centre” width=”auto” height=”90%” controls>
<source src=”movies/VARIABLE FILE NAME HERE” type=”video/mp4″>
</video>
[/code]

In php I would echo the variable into the src so in javascript I thought I could use document.write(file); however that didnt work and after a bit of research I found that using document.write is bad practice.

All help greatly appreciated.
Thanks

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Troy_IIIMar 13.2015 — Try this:

[code=php]var media = document.all.tags("video");
media.centre ? media.centre.src = file : 0;
media.centre.play();//optional[/code]
Copy linkTweet thisAlerts:
@Troy_IIIMar 15.2015 — Will work on IE ?
×

Success!

Help @perky416 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...