8 Replies - 6269 Views - Last Post: 11 December 2008 - 08:13 AM

#1 Arenlor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 389
  • Joined: 26-April 06

onclick and window.location doesn't work

Post icon  Posted 09 December 2008 - 01:10 AM

The onclick contains this
bank = confirm("Press cancel to go to the bank before you upgrade!");if(bank==true){window.location = "http://www.robowarsgame.com/beta199b/game/bios.php";}else{window.location = "http://www.robowarsgame.com/beta199b/game/bank.php";}

The if/else works fine, it's the window.location that has trouble. It's inserted into the code by GreaseMonkey, but as I said, the if/else works fine, I tested them with alert(). So I'm thinking I did something wrong with the window.location?

Is This A Good Question/Topic? 1

Replies To: onclick and window.location doesn't work

#2 Winstinology  Icon User is offline

  • I'm pretty good with a laser mouse..
  • member icon

Reputation: 7
  • View blog
  • Posts: 958
  • Joined: 08-May 08

Re: onclick and window.location doesn't work

Posted 09 December 2008 - 12:59 PM

window.loaction is a bit weird I've found..
It works fine for me the way you have it for some reason..
Try calling it to a function in the <head> section and instead of using the 'window.location', try the following.

<script type="text/javascript">

   function (checkBlank)
   {
	  if (blank == true)
   {
	  location.href("http://www.yourURLHere.com");
   }
   else
   {
	  location.href("http://www.AnotherURLHere.com");
   }
}
</script>


This post has been edited by Winstinology: 09 December 2008 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 Arenlor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 389
  • Joined: 26-April 06

Re: onclick and window.location doesn't work

Posted 09 December 2008 - 10:01 PM

It still doesn't work, I also have now tried window.location.href, the new code I have is
var a = document.getElementsByTagName("a");
var script = document.createElement('script');
document.getElementsByTagName('head')[0].appendChild(script);
script.language='javascript';
script.type='text/javascript';
var newScript = 'function bank(){var bank = confirm("Press cancel to go to the bank before you upgrade!");if(bank==true){window.location.href = "http://www.robowarsgame.com/beta199b/game/bios.php";}else{window.location.href = "http://www.robowarsgame.com/beta199b/game/bank.php";}}';
script.innerHTML = newScript;
for (i=0;i<a.length;i++){
    if(a[i].href.match("bank.php")=="bank.php"){
        a[i].setAttribute("onclick","bank();");
    }
}

I can't figure it out, maybe it's just because of FireFox?
Was This Post Helpful? 0
  • +
  • -

#4 joezim007  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 110
  • Joined: 13-September 08

Re: onclick and window.location doesn't work

Posted 09 December 2008 - 11:19 PM

Your original code is working for me (in Firefox and IE). It might have something to do with it being put into GreaseMonkey. They may not allow URL redirects. IDK.
Was This Post Helpful? 0
  • +
  • -

#5 Findarato  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 35
  • Joined: 11-November 08

Re: onclick and window.location doesn't work

Posted 10 December 2008 - 05:07 AM

The original code seems fine to me aswell, the problem should be coming from elsewhere. Maybe put in some more explicit code for us to work with?
Was This Post Helpful? 0
  • +
  • -

#6 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: onclick and window.location doesn't work

Posted 10 December 2008 - 06:04 AM

The code does work perfectly. If you are using GM how are you adding this as an onclick? I assume it is through DOM access? If you place the code from your first post into a new GM script it will run on page load and do exactly what it should, so you will need to be more specific about how the script works.
GM code is only active during and immediately after a page loads, if you do not explicitly add the code it is not accessible.
Was This Post Helpful? 0
  • +
  • -

#7 Winstinology  Icon User is offline

  • I'm pretty good with a laser mouse..
  • member icon

Reputation: 7
  • View blog
  • Posts: 958
  • Joined: 08-May 08

Re: onclick and window.location doesn't work

Posted 10 December 2008 - 11:34 AM

So we're all at a consensus that the original code functions correctly. I agree, maybe post some more descriptive code..
Was This Post Helpful? 0
  • +
  • -

#8 Arenlor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 389
  • Joined: 26-April 06

Re: onclick and window.location doesn't work

Posted 11 December 2008 - 05:01 AM

The part that's confusing me is that I can get an alert() to work. It's the window.location (location.href and window.location.href also) that doesn't work.
Was This Post Helpful? 0
  • +
  • -

#9 joezim007  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 110
  • Joined: 13-September 08

Re: onclick and window.location doesn't work

Posted 11 December 2008 - 08:13 AM

View PostArenlor, on 11 Dec, 2008 - 04:01 AM, said:

The part that's confusing me is that I can get an alert() to work. It's the window.location (location.href and window.location.href also) that doesn't work.


I understand that but those are working for us so there is something else wrong. I think that GreaseMonkey might block window.location in scripts. I know that if you try to edit your myspace page and add ANY scripts into it, they're blocked so GreaseMonkey might be doing the same thing except being a little more specific.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1