• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Toggling Javascripts

[lhomme]

New Member
Is there a way to let users toggle a javascript on and off - with on being the default? Preferably, this toggling of the javascript would remain permanent unless toggled again by the user.

As always, thanks again.
 
You mean from the page? No, that would defeat the purpose of being able to turn it off. If you mean from the browser, of course. In Mozilla, for example, Edit > Preferences > Advanced > Scripts & Windows uncheck "Enable JavaScript for:" "Navigator" and "Mail & Newsgroups".

If you just want to toggle the scripts on your own page, though, that could be done. You'd just have to build in some kind of switch in each of them that would check the value of some form element on the page then either continue with the script or stop running. It wouldn't really disabled JavaScript, but it would seem to.

There might be some security flaw you can exploit in IE 5.x that would let you really do it, but I doubt it would work on 6, though, and it certainly wouldn't work on anything else.
 
Whoops - seems I wasn't being clear.

I have a (one) javascript on my page that I want users to be able to choose whether or not to leave on or turn off. I assume that since I want to the change to be permanent (until toggled on), that I would need to use cookies. Any ideas?
 
You could use cookies, yes, or just pass the on/off value through the query string of all your links. The cookie would be easier, but the other way would work for people that disallow cookies.

Code:
<script language="JavaScript"><!--
if(!document.cookie.match("script=off")){
	(script here)
}
//--></script>

<a href="#" onClick="document.cookie='script=on';">on</a>
<a href="#" onClick="document.cookie='script=off';">off</a>
 
Back
Top