View Full Version : Can I put a javascript in a CSS?

12-30-2005, 05:07 AM
If I add/create a javascript in notepad and save the file as .js, will this be a real javascript file? If I pute this file on a web site, can I put a code something like this (<SCRIPT LANGUAGE="JavaScript" c="popup.js"></SCRIPT>) in the head of my web page to call the script? The sample code I put up dosen't work. Will a Javascript work in a CSS page? I have several pages that will use the same code and I would like to use an off page script.

12-30-2005, 08:23 AM
at first i thought this was a joke, and i was going to give a funny sarcastic response, but i did a google search and i found out that it is possible, but has huge limitations, essentially its worthless for anything deep but here is a link


12-30-2005, 09:31 PM
If all your trying to do is have more than one page share the same javascript then just link to it.

<script type="text/javascript" src="whatever.js"></script>

^^ you can put that <script> tag any number of pages and they will all execute whatever.js

12-31-2005, 03:05 AM
Your page on browsers was very interesting. I bookmarked it so I can read it again. There's a lot there. Your research on my CSS/script is interesting. I would advise anyone here to visit your page.

I will try the script you suggested. I hope it works. It will save me a lot of time.

Thanks for the advice.

12-31-2005, 03:54 AM
I can't seem to get <script type="text/java script" src="popup.js"></script> to work. Maybe my script file isn't right. The script works well on the page but it won't from an outside page. I'm using a popup script I got from this site. I copied the script into Notepad and saved it with a .js extension. I added the script file in my website with my HTM files. I called it "popup.js" and it shows as a js file icon. Part of the script gos in the head and the other part gos in the body. I put the head part of the script in the file and I put the body part of the script on the page that the script is called to.

12-31-2005, 10:47 PM
post the code for popup.js
Note you do not put the script tags in an external js file.

01-01-2006, 02:05 AM
I copied the script below into notepad and saved the file with a .js extension instead of the Tex extension.

<script type="text/javascript">

// JK Pop up image viewer script- By JavaScriptKit.com
// Visit JavaScript Kit (http://javascriptkit.com)
// for free JavaScript tutorials and scripts
// This notice must stay intact for use

var popbackground="lightskyblue" //specify backcolor or background image for pop window
var windowtitle="Image Window" //pop window title

function detectexist(obj){
return (typeof obj !="undefined")

function jkpopimage(imgpath, popwidth, popheight, textdescription){

function getpos(){
leftpos=(detectexist(window.screenLeft))? screenLeft+document.body.clientWidth/2-popwidth/2 : detectexist(window.screenX)? screenX+innerWidth/2-popwidth/2 : 0
toppos=(detectexist(window.screenTop))? screenTop+document.body.clientHeight/2-popheight/2 : detectexist(window.screenY)? screenY+innerHeight/2-popheight/2 : 0
if (window.opera){

var winattributes='width='+popwidth+',height='+popheight+',resizable=yes,left='+leftpos+',top='+toppos
var bodyattribute=(popbackground.indexOf(".")!=-1)? 'background="'+popbackground+'"' : 'bgcolor="'+popbackground+'"'
if (typeof jkpopwin=="undefined" || jkpopwin.closed)
//getpos() //uncomment these 2 lines if you wish subsequent popups to be centered too
//jkpopwin.moveTo(leftpos, toppos)
jkpopwin.resizeTo(popwidth, popheight+30)
jkpopwin.document.write('<html><title>'+windowtitle+'</title><body '+bodyattribute+'><img src="'+imgpath+'" style="margin-bottom: 0.5em"><br>'+textdescription+'</body></html>')



I added the script call line in the "head"
<script type="text/javascript" src="popup.js"></script>

And I added the second part of the script in the "body"
- <a href="#" onClick="jkpopimage('images/rescue.jpg', 200, 300, 'rescue of a small whale'); return false">whale rescue</a><br>

Maybe, you would like to see the test page. It's at this location:
The script is in this page.

01-01-2006, 04:38 AM
Like metmmsc said. You don't put <script></script> tags in external javascript.

01-01-2006, 04:39 AM
OOPS i meant like aerospace said.. Sorry!

01-02-2006, 01:59 AM
WOW!! I see the light. My problem was that I had <script></script> in my external js file. I took <script></script out of the js file and it works. I no longer get error messages. The computer reads the page and when it comes to <script type="text/javascript" SEC="pop up.J's"></script>, the J's file is read into the HTML file and the script's function is carried out. Adding <script></script> to both the HTML file and the js file is like a double negative. Thanks again. This is a great forum with allot of talent.