Jump to content

get info from URL and set as a javascript variable


jarvis

Recommended Posts

Hi All,

 

Im trying to use PHP to get some info from URLs, once I have the data, I wish to set it in a seperate javascript file as a variable. Firstly, is this even possible?

 

My links are like so:

http://www.facebook.com/#!/video/video.php?v=

http://www.youtube.com/watch?v=

 

I wish to use PHP to get the link type, either facebook or youtube. Like so:

var URLtype = "";

 

So I can then build a dynamic flash media player but need it for this line:

 

html += '<embed src="http://www.'+URLtype+'.com/v/'+id+autoPlay+showRelated+fullScreen+'"';

 

I hope someone can help as I can't seem to work it out :-(

 

TIA

 

jarvis

Link to comment
Share on other sites

if you do something like this:

<div id="divname">someValue</div>

then you can grab the value into javascript with something like

 

var text1 = document.getElementById("divname").innerHTML;

 

or you can use hidden form elements and grab the elements value from javascript.

 

So in short, yes, it's possible to get a php value into javascript, but you need to make sure the value is set and placed in a place where you can access it from js before your javascript code runs.

Link to comment
Share on other sites

<?php
$url = 'http://www.facebook.com/#!/video/video.php?v=';

$temp = (parse_url($url));
$host = $temp['host'];
if ($host === 'www.facebook.com')
{
$URLtype = 'facebook';
}
else if ($host === 'www.youtube.com')
{
$URLType = 'youtube';
}
?>
<html>
<head><title></title>
<script type="text/JavaScript"
var URLType = <?php echo isset($URLType) ? $URLType : ''; ?>;
// then just use ur URLType variable in JS...
</script>
</head>
<body></body>
</html>

Link to comment
Share on other sites

Thanks Webstyles, I think that makes sense! I've set a div:

<div id="ytvideo"></div>

This shows the video selected from a list of videos in the main player. I've then added this line to my seperate js file:

var theURL = document.getElementById("ytvideo").innerHTML;

Then I altered the 2 lines in my emved code like so:

html += '<param name="movie" value="http://www.'+theURL+'.com/v/'+id+autoPlay+showRelated+fullScreen+'"> </param>';

AND

html += '<embed src="http://www.'+theURL+'.com/v/'+id+autoPlay+showRelated+fullScreen+'"';

This seperate file is called from the other doc

<script src="js/jquery.youtubeplaylist.js" type="text/javascript"></script>	

 

Sadly, it doesn't seem to work. Is is because it's a seperate js doc or will that line you kindly recommended retrieve the info regardless?

TIA

Link to comment
Share on other sites

Thanks TeNDoLLA but it's not getting the main URL it's from one within the page - sorry, I should've made that clear.

 

It's so I can use this:

http://www.geckonewmedia.com/blog/2009/8/14/jquery-youtube-playlist-plugin---youtubeplaylist but for facebook and youtube. However, I'm trying to make it dynamic with a php cms hence why I'm on this forum :-) I think it may be more js though :-(

Link to comment
Share on other sites

var theURL = document.getElementById("ytvideo").innerHTML;

 

will only work if there's actually some text inside your <div>

if you do this:

<div id="ytvideo">www.facebook.com</div>

then your javascript var theURL should grab the value www.facebook.com

Link to comment
Share on other sites

<?php
$url = 'http://www.facebook.com/#!/video/video.php?v=';

if ( stristr($host, 'facebook') )
{
   $URLtype = 'facebook';
}
else if ( stristr($host, 'youtube') )
{
   $URLType = 'youtube';
}
?>
<html>
<head><title></title>
<script type="text/JavaScript"
var URLType = <?php echo isset($URLType) ? $URLType : ''; ?>;

</script>
</head>
<body></body>
</html>

 

stristr

Link to comment
Share on other sites

<div id="ytvideo"></div>

is just an empty div with a name. it won't have any content unless you put some into it. if you have a php variable with the content you wish to pass on to js, just echo it. this is just one way of doing it, and unless you set the div to hidden, the url will display on your page.

 

<div id="ytvideo"><?php echo $url;?></div>

 

alternatively you can also use hidden form elements. And I'm quite sure Andy-H's example should work fine too.

Link to comment
Share on other sites

Now I am a bit confused what is your goal and what you got now? Also instead of this

var contentInElement = document.getElementById("ytvideo").innerHTML;

 

You should just use the jquery functions since you are already using the jquery anyway

var contentInElement = ('#ytvideo').html();

 

but this aint giving any URL for you with the HTML you are having though.

Link to comment
Share on other sites

Show your full code for the 2 pages involved.

 

 

You know you can save your javascript file as file.js.php and run php in it?

 

 

test.js.php

<?php
$url = 'http://www.facebook.com/#!/video/video.php?v=';

if ( stristr($host, 'facebook') )
{
   $URLtype = 'facebook';
}
else if ( stristr($host, 'youtube') )
{
   $URLType = 'youtube';
}
?>
$(document).ready(function() {
   var URLType = <?php echo $URLType; ?>;
   alert(URLType);
});

 

 

index.php

<html>
<head>
   <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
   <script type="text/javascript" src="test.js.php"></script>
</head>
<body>
</body>
</html>

Link to comment
Share on other sites

Thanks again WebStyles and Andy-H.

Here's the webpage:

	<!-- main loading area -->
	<div id="ytvideo"></div>
	<!-- /main loading area -->

	<!-- youtube library holder -->
	<div class="yt_holder">	

	<div class="yt_holder_title">More films</div>

			<!-- video library -->
			<ul class="demo">				
			<li><a href="http://www.facebook.com/#!/video/video.php?v=123">Video 1</a></li> 	
			<li><a href="http://www.facebook.com/#!/video/video.php?v=456">Video 2</a></li>
			<li><a href="http://www.facebook.com/#!/video/video.php?v=789">Video 3</a></li> 	
			<li><a href="http://www.youtube.com/watch?v=w0ffwDYo00Q">Video 4</a></li>  															
			</ul>
			<!-- video library -->

	</div>
	<!-- /youtube library holder -->	

At the top of the page is the following js include:

	<script src="js/jquery.youtubeplaylist.js" type="text/javascript"></script>		
	<script type="text/ecmascript">
			$(function() {
				$("ul.demo").ytplaylist({addThumbs:true, autoPlay: false, holderId: 'ytvideo'});
			});
	</script>

The js file jquery.youtubeplaylist.js has the following:

jQuery.fn.ytplaylist = function(options) {

  // default settings
  var options = jQuery.extend( {
    holderId: 'ytvideo',
playerHeight: '258',
playerWidth: '399',
addThumbs: false,
thumbSize: 'small',
showInline: false,
autoPlay: true,
showRelated: true,
allowFullScreen: false
  },options);

  return this.each(function() {

   		var selector = $(this);
	var theURL = document.getElementById("ytvideo").innerHTML;
	var autoPlay = "";
	var showRelated = "&rel=0";
	var fullScreen = "";
	if(options.autoPlay) autoPlay = "&autoplay=1"; 
	if(options.showRelated) showRelated = "&rel=1"; 
	if(options.allowFullScreen) fullScreen = "&fs=1"; 

	//throw a youtube player in
	function play(id)
	{
	   var html  = '';

	   html += '<object height="'+options.playerHeight+'" width="'+options.playerWidth+'">';
	   html += '<param name="movie" value="http://www.'+theURL+'.com/v/'+id+autoPlay+showRelated+fullScreen+'"> </param>';
	   html += '<param name="wmode" value="transparent"> </param>';
	   if(options.allowFullScreen) { 
	   		html += '<param name="allowfullscreen" value="true"> </param>'; 
	   }
	   html += '<embed src="http://www.'+theURL+'.com/v/'+id+autoPlay+showRelated+fullScreen+'"';
	   if(options.allowFullScreen) { 
	   		html += ' allowfullscreen="true" '; 
	   	}
	   html += 'type="application/x-shockwave-flash" wmode="transparent"  height="'+options.playerHeight+'" width="'+options.playerWidth+'"></embed>';
	   html += '</object>';

	   return html;
	   
	};


	//grab a youtube id from a (clean, no querystring) url (thanks to http://jquery-howto.blogspot.com/2009/05/jyoutube-jquery-youtube-thumbnail.html)
	function youtubeid(url) {
		var ytid = url.match("[\\?&]v=([^&#]*)");
		ytid = ytid[1];
		return ytid;
	};


	//load inital video
	var firstVid = selector.children("li:first-child").addClass("currentvideo").children("a").attr("href");
	$("#"+options.holderId+"").html(play(youtubeid(firstVid)));

	//load video on request
	selector.children("li").children("a").click(function() {

		if(options.showInline) {
			$("li.currentvideo").removeClass("currentvideo");
			$(this).parent("li").addClass("currentvideo").html(play(youtubeid($(this).attr("href"))));
		}
		else {
			$("#"+options.holderId+"").html(play(youtubeid($(this).attr("href"))));
			$(this).parent().parent("ul").find("li.currentvideo").removeClass("currentvideo");
			$(this).parent("li").addClass("currentvideo");
		}



		return false;
	});

I know that the container with id=ytvideo will remain empty at all times and is just a reference point for the js (above).  I somehow need to grab the URL of the selected video in order to determine if it's a facebook URL or youtube URL. That way I can get the code to update the flash player in the js code. Otherwise I can only get it to work with one or other due to this line:

html += '<embed src="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'"';

 

Does that help?

Thanks, your patience & assistance is really REALLY appreciated!! :-)

Link to comment
Share on other sites

It's purely been done that way so we can style the page how we want, give the effect we're after and equally, it's going to be a dynamic list, so the client can upload the facebook or youtube URL.  This way seemed the best option (at the time!). The code we have seems simple enough, I really didnt think it'd be this cumbersome trying to detect a facebook or youtube link you see :-(

Link to comment
Share on other sites

 

//-------------------------------------------------
//		youtube playlist jquery plugin
//		Created by dan@geckonm.com
//		www.geckonewmedia.com
//
//		v1.1 - updated to allow fullscreen 
//			 - thanks Ashraf for the request
//-------------------------------------------------


jQuery.fn.ytplaylist = function(options) {

  // default settings
  var options = jQuery.extend( {
    holderId: 'ytvideo',
playerHeight: '300',
playerWidth: '450',
addThumbs: false,
thumbSize: 'small',
showInline: false,
autoPlay: true,
showRelated: true,
allowFullScreen: false
  },options);

  return this.each(function() {

   		var selector = $(this);

	var autoPlay = "";
	var showRelated = "&rel=0";
	var fullScreen = "";
	if(options.autoPlay) autoPlay = "&autoplay=1"; 
	if(options.showRelated) showRelated = "&rel=1"; 
	if(options.allowFullScreen) fullScreen = "&fs=1"; 

	//throw a youtube player in
	function play(optionVars)
	{
           var options   = optionVars.split('|');
           var id        = options[0];
           var urlType   = options[1];
	   var html  = '';
	   html += '<object height="'+options.playerHeight+'" width="'+options.playerWidth+'">';
	   html += '<param name="movie" value="http://www.' + urlType + '.com/v/'+id+autoPlay+showRelated+fullScreen+'"> </param>';
	   html += '<param name="wmode" value="transparent"> </param>';
	   if(options.allowFullScreen) { 
	   		html += '<param name="allowfullscreen" value="true"> </param>'; 
	   }
	   html += '<embed src="http://www.' + urlType + '.com/v/'+id+autoPlay+showRelated+fullScreen+'"';
	   if(options.allowFullScreen) { 
	   		html += ' allowfullscreen="true" '; 
	   	}
	   html += 'type="application/x-shockwave-flash" wmode="transparent"  height="'+options.playerHeight+'" width="'+options.playerWidth+'"></embed>';
	   html += '</object>';

	   return html;
	   
	};


	//grab a youtube id from a (clean, no querystring) url (thanks to http://jquery-howto.blogspot.com/2009/05/jyoutube-jquery-youtube-thumbnail.html)
	function youtubeid(url) {
		var ytid   = url.match("[\\?&]v=([^]*)");
            var regex  = /(facebook|youtube)/i;
            var type   = regex.exec(url);
		ytid = ytid[1];
		return ytid + '|' + type[0];
	};


	//load inital video
	var firstVid = selector.children("li:first-child").addClass("currentvideo").children("a").attr("href");
	$("#"+options.holderId+"").html(play(youtubeid(firstVid)));

	//load video on request
	selector.children("li").children("a").click(function() {

		if(options.showInline) {
			$("li.currentvideo").removeClass("currentvideo");
			$(this).parent("li").addClass("currentvideo").html(play(youtubeid($(this).attr("href"))));
		}
		else {
			$("#"+options.holderId+"").html(play(youtubeid($(this).attr("href"))));
			$(this).parent().parent("ul").find("li.currentvideo").removeClass("currentvideo");
			$(this).parent("li").addClass("currentvideo");
		}



		return false;
	});

	//do we want thumns with that?
	if(options.addThumbs) {

		selector.children().each(function(i){
										  
			var replacedText = $(this).text();

			if(options.thumbSize == 'small') {
				var thumbUrl = "http://img.youtube.com/vi/"+youtubeid($(this).children("a").attr("href"))+"/2.jpg";
			}
			else {
				var thumbUrl = "http://img.youtube.com/vi/"+youtubeid($(this).children("a").attr("href"))+"/0.jpg";
			}


			$(this).children("a").empty().html("<img src='"+thumbUrl+"' alt='"+replacedText+"' />"+replacedText).attr("title", replacedText);

		});	

	}


   
  });

};

 

 

Try this (untested) defo wont work for thumb images.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.