views:

192

answers:

2

Hello all,

I am trying to make use of Google's API as a way to get the location of the user. Once I have done this, I pass this to an external PHP script which will further output some JavaScript code. However, I am having trouble calling the PHP script:

<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAKw7Q"&gt;&lt;/script
<script type="text/javascript">
    if(google.loader.ClientLocation)
    {
        visitor_countrycode = google.loader.ClientLocation.address.country_code;
    }
</script>
<script type='text/javascript' src='http://www.mysite.com/widget.php?mid=12&amp;c=visitor_countrycode'&gt;
</script>

The above is what is retreived from my DB. However the variable visitor_countrycode does not get generated in the HTML it still contains the string "visitor_countrycode" rather than its Javascript value.

I just can't figure it out.

Update

I actually can use JQuery:

I have tried this but I didn't get much luck with it.

$("<script type='text/javascript' scr='http://www.mysite.com/widget.php?mid=12&amp;c="+visitor_countrycode+"'").appendTo('body');

Anything wrong with it?

+3  A: 

Right, well this line:

<script type='text/javascript' src='http://www.mysite.com/widget.php?mid=12&amp;c=visitor_countrycode'&gt;
</script>

...is simply retrieving the URL, "http://www.mysite.com/widget.php?mid=12&amp;c=visitor_countrycode". The variable isn't being evaluated -- it's being passed as a plain parameter.

If you want to grab a dynamically-generated URL, you have to create a new <script> element and append it to the head. Like so:

var visitor_countrycode = 'foo';

// create the new script element
var script_element = document.createElement('script');

// visitor_countrycode will be evaluated here.
script_element.src = 'http://www.mysite.com/widget.php?mid=12&amp;c=' + visitor_countrycode;

// this gets the <head>, and then appends the newly-created script element.
document.getElementsByTagName('head')[0].appendChild(script_element);

Voila.

bigmattyh
Awesome idea. I tried it but I got: "document.createNode is not a function" from firebug. I must be doing something wrong?
Abs
gah, try createElement instead.
bigmattyh
Will this work in IE as well? Also I tried this but the JS code doesn't appear in the head. The one that the PHp script should output. I guess it shouldn't but what I want to appear doesn't appear. Is there anything else I need to make sure of.
Abs
Isn't it suppose to be .src and not .href on the script element?
AntonioCS
Yep, that did the job. Will this work across all browsers though?
Abs
I have received the output of the external php script. However, that output doesn't appear. I have changed to append to body. Do I need to use write to?
Abs
Yes, it was supposed to be "src", not "href". This is what happens when you type out code on the fly and don't test. :)
bigmattyh
Abs, yes, it should work in any modern browser. If you are using jQuery or some other library that is meant to abstract out the differences between browsers, you can use the same approach (adding a script element to the head), but adjusting your syntax accordingly.
bigmattyh
Awesome! Thank you very much bigmattyh. :)
Abs
A: 

You could use a simple HTTP request to call that PHP script as well, that way you could even take the result and do something based on whether the PHP script handled your request correctly or not.

<script type="text/javascript">
var http = false;

if(google.loader.ClientLocation)
{
    visitor_countrycode = google.loader.ClientLocation.address.country_code;

    if(navigator.appName == "Microsoft Internet Explorer") {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        http = new XMLHttpRequest();
    }

    http.open("GET", 'http://www.mysite.com/widget.php?mid=12&amp;c=' + visitor_countrycode);
    http.send(null);
}
</script>
tijs