Jump to content

Javascript Bar (include, variables & ifs)

Recommended Posts

I have two files that currently use PHP, but I'd like to convert it into javascript so that it can be used on non PHP sites.These are the two files:example.php

<?php// Bar$youtube="";	// YouTube username$facebook="";	// Facebook username$twitter="";	// Twitter username?><!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>PHP</title></head><body>	<?php include ("bar.php"); ?></body></html>


<!-- CSS begin --><link rel="stylesheet" type="text/css" href="bar.css" /><!-- CSS end --><!-- Bar begin --><div id="bar">	<!-- YouTube -->	<?php if (isset($youtube)) {		echo "<a href=\"http://www.youtube.com/user/".$youtube."\" target=\"_blank\"><div id=\"social\" class=\"youtube\"></div></a>";	} ?>	<!-- Facebook -->	<?php if (isset($facebook)) {		echo "<a href=\"http://www.facebook.com/".$facebook."\" target=\"_blank\"><div id=\"social\" class=\"facebook\"></div></a>";	} ?>	<!-- Twitter -->	<?php if (isset($twitter)) {		echo "<a href=\"http://twitter.com/#!/".$twitter."\" target=\"_blank\"><div id=\"social\" class=\"twitter\"></div></a>";	} ?>	<noscript>		<p>Please enable JavaScript.</p>	</noscript></div><!-- Bar end -->


Link to post
Share on other sites
Which part do you need help with? You can declare variables in Javascript similarly to PHP, and you can replace the echo statements with document.write to output the links where you want them.
I just need the codes to do that as I'm not very experienced with Javascript…
Link to post
Share on other sites

Okay, so I've created the variables.example.php

<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>PHP</title>	<!-- Javascript begin -->	<script>	// Plug In Studios Bar	// Replace 'NULL' with '"example"' to enable the feature.	var message=NULL	// Message	var youtube=NULL	// YouTube username	var facebook=NULL	// Facebook username	var twitter=NULL	// Twitter username	var rssURL=NULL	// RSS URL	var email=NULL		// Email addrss		</script>	<!-- Javascript end --></head><body>	<?php include ($_SERVER['DOCUMENT_ROOT']."/bar/includes/bar.php"); ?></body></html>

Although how do I use a javascript include to replace

<?php include ($_SERVER['DOCUMENT_ROOT']."/bar/includes/bar.php"); ?>

and then use the if statements to determine if the variable exists and then include the variables.

Link to post
Share on other sites

You can't really include a file with Javascript the same way you do with PHP. The closest you can get is to use an ajax request to get the contents of another Javascript file and then use eval on the contents to execute the code. You would need to make sure the request is synchronous so that it loads the file and then executes the new code in the same place on the page, so that if you have document.write statements in the loaded code they will print the text in the correct area on the page (wherever the ajax code is).If you want to do that, the Javascript file you load through ajax needs to only have Javascript code, no HTML tags. So the file would look something like this:

document.write('<link rel="stylesheet" type="text/css" href="bar.css" />');document.write('<div id="bar">');if ((typeof youtube) != 'undefined' && youtube != null) {  document.write("<a href=\"http://www.youtube.com/user/" + youtube + "\" target=\"_blank\"><div id=\"social\" class=\"youtube\"></div></a>");}document.write('</div>');

There's an overview of ajax communication here:http://www.w3schools.com/ajax/ajax_intro.aspThis page describes sending a request:http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.aspAs you can see from that page, to make sure the request is synchronous instead of asynchronous you need to set the third parameter to the open method to false. The method can still be get, and the URL will be the Javascript file to load.

Link to post
Share on other sites

Join the conversation

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

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...