Forum Moderators: open

Message Too Old, No Replies

Url and Source

         

gameoverload

4:34 am on Dec 22, 2009 (gmt 0)

10+ Year Member



I am fairly new with javascript (and this forum), but am very familiar with html.
I have created a gaming website, in an effort to get through schools filters.
I want to have an "add to your web-page" function for the games and have created an external java-script file for each game (there is about a hundred so it took a while, however, places like myspace dont allow javascript, so i want to make a frame that has the javascript in it. However, I am lazy and dont want to create a frame for each game, so instead it would be
"http://example.com/frames.html?game=the_impossible_quiz"
the game variable would change according to the game. So far i have placed this together:

<script type="text/javascript">
window.$_GET=(function(){
var get_array=new Object();
if(!location.search)return get_array;
var p=location.search.replace(/\+/g,"%20").substr(1).split("&"),l=p.length;
for(var i=0,t;i<l;i++){
t=p[i].split("=");
if(!t[0])t[0]="";
if(!t[1])t[1]="";
t[0]=decodeURIComponent(t[0]);
t[1]=decodeURIComponent(t[1]);
get_array[t[0]]=t[1];
}
return get_array;
})();
</script>
<script type="text/javascript">
alert($_GET['game']);
</script>

The game name alerts fine, but i need it to be
(script source =)http://example.com/embed/ "game name" +.js

I could also do
embed src= http://www..... "game name" .swf
But I would like to learn the other way,

Yes, I am aware I am using a free domain, a .com with the word "game" in it sets off the schools firewall, that and i don't want to pay for one (I'm cheap)

Thanks!

[edited by: Fotiman at 5:31 pm (utc) on Dec. 22, 2009]
[edit reason] Examplified URLs [/edit]

whoisgregg

7:34 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, gameoverload!

If you are trying to embed a script from another site, you can dynamically add a new <script> element to the <head> element. Something along these lines should work:

var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.src = 'http://www.example.com/embed/' + $_GET['game'] + '.js';
document.documentElement.getElementsByTagName("HEAD")[0].appendChild(newScript);

gameoverload

7:17 am on Dec 22, 2009 (gmt 0)

10+ Year Member




System: The following message was spliced on to this thread from: http://www.webmasterworld.com/javascript/4047422.htm [webmasterworld.com] by whoisgregg - 5:12 pm on Dec. 22, 2009 (est -5)


I have finally made the code, but it doesn't work take a look:
<body>

<script type="text/javascript">
function get_search_string()
{
var fullurl=window.location.search
var param=(fullurl.slice(1))

var pairs = param.split("&")
var targetvar = "game"

for(var i = 0; i < pairs.length; i++)
{
var pos = pairs[i].indexOf('=')
if (pos == -1) continue
var argname = pairs[i].substring(0,pos)
var value = pairs[i].substring(pos+1)
if (argname == targetvar)
{
var searchstring = value
{break}
}
}

return searchstring
}
</script>
<script type="text/javascript">
var domain = "http://example.com/embed/"
var file = ".js"
var embed = domain + value + file
</script>
<script type="text/javascript">
document.write('<sc'+'ript');
document.write(' type="text/javascript"');
document.write(' language="JavaScript"');
document.write(''+embed+'">');
document.write('</sc'+'ript>');
</script>

<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>
<p>TEST</p>
</body>

TEST does appear

You can try it at
SNIP

Thanks in advance, any help counts!

[edited by: Fotiman at 5:38 pm (utc) on Dec. 22, 2009]
[edit reason] No URLs please. See TOS [webmasterworld.com] [/edit]

gameoverload

9:38 pm on Dec 23, 2009 (gmt 0)

10+ Year Member



Thanks for the reply, it works great!
This script came in handy, I am redoing my navigation and some pages, and this script is making it a breeze!