quick javascript help?

affluent

New member
Nov 18, 2009
204
4
0
What am I doing wrong here...? =/

<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

if(Math.floor(Math.random()*2) == 1)
window.location="zzz2.com/?zip=" + geoip_postal_code();
else
window.location="zzz1.com/?zip=" + geoip_postal_code();
</SCRIPT>
</HEAD>

for some reason the zip code doesnt show up when i am redirected to either page. not sure what im doing wrong :angryfire:

im sure this will take one second for you js experts to figure out. lol thanks in advance
 


Might be better if you tried not to do so much stuff inline

Code:
var RandNum = Math.floor(Math.random()*2);
var PostalCode = geoip_postal_code(); //assuming you loaded this function already
var Destination = "";

if(RandNum == 1) { 
   Destination = "http://zzz2.com/?zip="+PostalCode;
} else {
   Destination = "http://zzz1.com/?zip="+PostalCode;
}

window.location = Destination;

Far as the zip code, you can always double check it by making it alert() the postal code instead of just doing it. I didn't see you include a javascript file or function in your code as such you want to make sure the function geoip_postal_code() has been loaded, you may even need to use JQuery or somehting similar so that you can do something like $(document).ready()... so that it doesn't attempt to run the redirection until after the page's components were completely loaded.
 
This bit doesn't make sense to me
Code:
if(Math.floor(Math.random()*2) == 1)
In English this is equivalent to;
* get a random number
* multiply it by 2
* round it down to the nearest whole integer (eg 10120.857883 to 10120)
* then test to see if it's equal to 1

I suspect you want to tell if you have an odd or even number, in which case you're code should look like this
Code:
if ((Math.random() % 2) == 1 ) {
    // random number is odd
} else {
    // random number is even
}
But in any case I still don't see what you're trying to achieve. Do you want to know if the geoip_postal_code() is odd or even?
Code:
if ((geoip_postal_code() % 2) == 1 ) {
    // number is odd
} else {
    // number is even
}
 
It doesn't show up because you don't have an event triggering the Javascript to fire. Javascript is not a server-side language like PHP.

Like kblessngr said, you will need to either switch to jQuery and use their ready function, or include the onload="" parameter to the body tag of the page. You will also need to make that script a function so it will be called when the page is loaded.

Really though, if you are wanting that to deal with data once the page is loaded, you need to use a server-side language since it's mission-critical. Not everyone's going to have Javascript enabled.

Oh, and you might want to download Firebug and use their console logging feature. Instead of the alert() function, you can use console.log(), which will print it to the Firebug console - allowing you to check stuff easier and more quietly, even in a live production environment so the user doesn't see you're testing it. It also allows you to use Firebug's JavaScript debuger.
 
Like kblessngr said, you will need to either switch to jQuery and use their ready function, or include the onload="" parameter to the body tag of the page. You will also need to make that script a function so it will be called when the page is loaded.


Well code all by itself inside of a javascript script block does run by itself on page load, just varies widely on the browser like some require the script block to be in the body, where as others will do so from the head, but might do so before all components on the document have been processed.
 
Well code all by itself inside of a javascript script block does run by itself on page load, just varies widely on the browser like some require the script block to be in the body, where as others will do so from the head, but might do so before all components on the document have been processed.

True, it's the last point you make that I was trying to cover. You really don't know with JavaScript which component it's going to load first, since some browsers try and thread JavaScript based on size or other factors.

I do think I see where he's running into an issue - the geoIP library is outside of the opening HEAD tag - which means that it's probably not even being loaded. Which, if he were using Firebug would have shown a non-existent variable when he tries to call the library's functions.
 
sigh i've tried everything. putting all the code in the <head> tags and <body> tags...even copy pasting kblessinggr's code and it still wont pass the zip code through.

fuck i hate javascript
 
What you are doing wrong is that you are calling functions on a script before verifying that you have access to the functions in the first place.

Save this as a html file and put it on the server:

Code:
<html>
<head>
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
</head>
<body>
<script language="javascript">
document.write("zzz1.com/?zip=" + geoip_postal_code());
</script>
</body>
</html>

visit it in the browser you should see:

Code:
zzz1.com/?zip=YOURZIPCODEHERE

if so, you can proceed.
 
What you are doing wrong is that you are calling functions on a script before verifying that you have access to the functions in the first place.

This should be your initial thought. Use the code jryan gave you. If the proper zip doesn't show up on the page then you're doing something wrong.

It's easy to hate Javascript but it's easier to hate yourself for writing it wrong in the first place.

I haven't ever used MaxMind but it seems as if you may have to download and install a database of info also.
 
But in any case I still don't see what you're trying to achieve. Do you want to know if the geoip_postal_code() is odd or even?
Code:
if ((geoip_postal_code() % 2) == 1 ) {
    // number is odd
} else {
    // number is even
}

Well he's not trying to figure if geocode is odd/even, he's trying to figure out if the random number is 1 or 2 , basically he randomly wants to switch the domain (note zzz1 and zzz2) for the redirection, probably splitting two offers on a zip submit.

Though the use of modula would be handy, would still need to generate a random number.
 
I haven't ever used MaxMind but it seems as if you may have to download and install a database of info also.

I always felt it was a bit easier to do that stuff via php anyways, which is pretty easy to do with the maxmind binary format file, though they do a csv that can be converted into mysql for bit faster local lookup, could probably even handle the destination url in php as well thus cutting down on the math functionality in JS.