/    Sign up×
Community /Pin to ProfileBookmark

The syntax of some javascript: help please

Hi there,

I have been given some code to use to toggle between enabling and disabling of Cascading Stylesheets. The only problem is that the code has been given to me to be used as is in a webpage link (see below):

[code=php]<a href=”javascript:var i=0;if(document.styleSheets.length>0){cs=!document.styleSheets[0].disabled;for(i=0;i<document.styleSheets.length;i++) document.styleSheets[i].disabled=cs;};void(cs=false);”>Toggle CSS</a>[/code]

Can someone tell me if this code can be used by including it in a global js file that I can call? If so how would I do that and is there a way in incorporate an alert box if js is disabled?

Many many thanks
Karl

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@PittimannJul 17.2004 — Hi!

This example assumes that test.css and test.js are in the same folder like this HTML document:[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<LINK href="test.css" type="text/css" rel="stylesheet">
<script language="JavaScript" type="text/javascript" src="test.js"></script>
</head>
<body>
<span style="font-size:20pt;font-weight:bold;"><noscript>Please note: JavaScript is disabled on your machine!</noscript></span>
<a href="#" onclick="toggleCSS();return false;">Toggle CSS</a>
</body>
</html>[/code]
The test.js has this code:[code=php]function toggleCSS(){
var i=0;
if(document.styleSheets.length>0){
cs=!document.styleSheets[0].disabled;
for(i=0;i<document.styleSheets.length;i++)
document.styleSheets[i].disabled=cs;
}
void(cs=false);
}[/code]
You cannot popup an alert, if js is disabled because js is needed for that. The noscript bit in the snippet above will display the message 'Please note: JavaScript is disabled on your machine!' if js is disabled.

Instead of using an anchor tag to call the toggle function, it could be anything capable of invoking a script.

Cheers - Pit
Copy linkTweet thisAlerts:
@KarlBauthorJul 17.2004 — Hi Pittimann

That is very kind of you. Thank you very much.

One day I will have the time to learn some of this ?

Karl
Copy linkTweet thisAlerts:
@PittimannJul 17.2004 — Hi!

You are welcome! ?

Cheers - Pit
×

Success!

Help @KarlB spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...