View Full Version : calling/making a .js file
JaeSun
08-27-2002, 11:22 PM
ok, im using this javascript, which i got from here:
http://www.htmlforums.com/showthread.php?s=&threadid=16327
im putting it in the header ... well, i have about 20 links i have to use for popups, and that means 20 functions for each link .. i obviously dont wanna put all functions in the header code of every page ....
how do i take that javascript and have it called from a .js file?
also, can this .js file be placed in the head also, or does it still have to be placed in the body?
JaeSun
08-27-2002, 11:31 PM
ok, i was looking around for sites that use the .js call up, and i decided to head to www.microsoft.com
i looked at their code and this is what it had for the code:
(i subsituted the <> for [])
2 questions:
1. is that what i do? put the src="" to the location of the .js file, and use notepad, put the javascript in, and save it as a .js file ?
2. i looked at microsoft's .js file ( www.microsoft.com/homepage/toolbar.js ), and i notice that they didnt use any opening and closing javascript codes ... like they didnt have any of this in the .js file:
code
they just went straight to the function blah() codes ...
so do i have to put the opening and closing script codes?
code
or do i just put the function code in the .js file?
scoutt
08-28-2002, 01:26 AM
all you had to do was ask :)
if you put the function in a js file then you don't use and <script> tags. just the function itself.
then in the page you call it in you just call it like you say.
<script language="JavaScript" src="/homepage/toolbar.js"></script>
Dr. Web
08-28-2002, 01:46 AM
eeeeerch! slow down jaesun. no need to put 20 seperate functions in. How about we use ONE function for all links? then you can call it externally or in the page.
Heres one example of this in use:
<html>
<head>
<title>pop-up</title>
<script language ="javascript">
function pop_up(where){
win = window.open(where, "dep", "height=800,600", hotkeys="no");
}
</script>
</head>
<h3>pop-up</h3>
<body>
<a href="#" onclick="pop_up('http://www.yahoo.com');">Yahoo</a>
<a href="#" onclick="pop_up('http://www.eye4u.com');">eye4u</a>
<a href="#" onclick="pop_up('/images/mypicture.jpg');">my picture</a>
</body>
</html>
JaeSun
08-28-2002, 11:04 AM
<html>
<head>
<title>pop-up</title>
<script language ="javascript">
function pop_up(where){
win = window.open(where, "dep", "height=800,600", hotkeys="no");
}
</script>
</head>
<h3>pop-up</h3>
<body>
<a href="#" onclick="pop_up('http://www.yahoo.com');">Yahoo</a>
<a href="#" onclick="pop_up('http://www.eye4u.com');">eye4u</a>
<a href="#" onclick="pop_up('/images/mypicture.jpg');">my picture</a>
</body>
</html>
ok, ill have to try that....does it take away the toolbar, the menubar etc also?
on the function part of the code, it has:
--------------
function pop_up(where){
win = window.open(where, "dep",
--------------
whats the where? and the "dep" do i have to change that?
Dr. Web
08-28-2002, 12:16 PM
not sure if all the menubars are gone. Just add a couple of things.... heres the changed code. DON'T change the function. THats the idea.... you don't change the internal working of the function... you change the values in the LINKS. Just look at the code, its pretty simple...
<html>
<head>
<title>pop-up</title>
<script language ="javascript">
function pop_up(where, name){
win = window.open(where, 'dep', "height=600, width=800, hotkeys=no, toolbar=no, menubar=no");
}
</script>
</head>
<h3>pop-up</h3>
<body>
<a href="#" onclick="pop_up('http://www.yahoo.com');">Yahoo</a>
<a href="#" onclick="pop_up('http://www.eye4u.com');">eye4u</a>
<a href="#" onclick="pop_up('/images/mypicture.jpg');">my picture</a>
</body>
</html>
Dr. Web
08-28-2002, 12:17 PM
btw: DEP is the name of the window you open. If you want all windows to open in DIFFERENT pop ups.... let me know. its a small change. Just paste this code in and play with it for a while.
JaeSun
08-28-2002, 12:26 PM
well, the thing is, several of the windows will be different sizes .... thats the thing ....
also, i would like to make it "centered" when the window pops up, well, not completely, but more like 2/3's (as the one code you fixed in the other thread did) .....
man, this is getting harder as i go ... im sorry, but thanks for all the help so far ...
but i only want the titlebar showing, and the window itself, no menubars, no address bar, no nothing ....
so if i can just get a code, like this, that:
centers (2/3's though, not complete center), no menubars, and i need it to be different sizes for some links
i think thatll be what i need ....
Dr. Web
08-28-2002, 02:29 PM
okay, heres a script that allows you to choose the url, windowName, height, width.
This should take care of you.... Scoutt posted the code already to center a pop up in your other thread.
<html>
<head>
<title>pop-up</title>
<script language ="javascript">
function pop_up(where, name, h, w){
options="toolbar=0, location=0, directories=0, status=0, menubar=0, resizable=0, width="+w+ " height="+h;
alert(options)
win = window.open(where, name, options);
}
</script>
</head>
<h3>pop-up</h3>
<body>
<a href="#" onclick="pop_up('http://www.yahoo.com', 'yahoo', 600, 800);">Yahoo</a>
<a href="#" onclick="pop_up('http://www.eye4u.com', 'eye', 768, 1024);">eye4u</a>
<a href="#" onclick="pop_up('/images/mypicture.jpg', 'myPic', 480, 600);">my picture</a>
</body>
</html>
JaeSun
08-28-2002, 03:03 PM
ok. thanks,
now the code to center thats in the other thread, im getting the idea that itll resize the window, then center it? or does it just center it 2/3s only?
thanks so much!!
im prob gonna use a .js file anyways, since i do have 2 other javascripts being called up in the head anyways ....
now, since in the .js file, i dont have to call up the opening and closing codes, do i just take out only the function part of the code, and put it in the .js file?
www.satworld.biz/new.shtml in the head portion of code
here is the current code:
<SCRIPT LANGUAGE="JavaScript">
<!--
function writeDate(){
var this_month = new Array();
this_month[0] = "January";
this_month[1] = "February";
this_month[2] = "March";
this_month[3] = "April";
this_month[4] = "May";
this_month[5] = "June";
this_month[6] = "July";
this_month[7] = "August";
this_month[8] = "September";
this_month[9] = "October";
this_month[10] = "November";
this_month[11] = "December";
var today = new Date();
var day = today.getDate();
var month = today.getMonth();
var year = today.getYear();
year <= 100 ? year += 1900 : 0;
var time = today.getHours();
if (time >= 12){
if (time > 12){time -= 12;}
today.getMinutes() < 10 ? time = time+":0"+today.getMinutes()+" PM" : time = time+":"+today.getMinutes()+" PM";
}else{
if (time == 0){time = 12;}
today.getMinutes() < 10 ? time = time+":0"+today.getMinutes()+" AM" : time = time+":"+today.getMinutes()+" AM";
}
return(this_month[month]+" "+day+", "+year);
}
//-->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function MakeArrayday(size) {
this.length = size;
for(var i = 1; i <= size; i++) {
this[i] = "";
}
return this;
}
function MakeArraymonth(size) {
this.length = size;
for(var i = 1; i <= size; i++) {
this[i] = "";
}
return this;
}
function funClock() {
if (!document.layers && !document.all)
return;
var runTime = new Date();
var hours = runTime.getHours();
var minutes = runTime.getMinutes();
var seconds = runTime.getSeconds();
var dn = "AM";
if (hours >= 12) {
dn = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}
if (minutes <= 9) {
minutes = "0" + minutes;
}
if (seconds <= 9) {
seconds = "0" + seconds;
}
movingtime = hours + ":" + minutes + ":" + seconds + " " + dn;
if (document.layers) {
document.layers.clock.document.write(movingtime);
document.layers.clock.document.close();
}
else if (document.all) {
clock.innerHTML = movingtime;
}
setTimeout("funClock()", 1000)
}
window.onload = funClock;
// End -->
</SCRIPT>
do i change it all to be this in the .js (with the update code that im adding):
function writeDate(){
var this_month = new Array();
this_month[0] = "January";
this_month[1] = "February";
this_month[2] = "March";
this_month[3] = "April";
this_month[4] = "May";
this_month[5] = "June";
this_month[6] = "July";
this_month[7] = "August";
this_month[8] = "September";
this_month[9] = "October";
this_month[10] = "November";
this_month[11] = "December";
var today = new Date();
var day = today.getDate();
var month = today.getMonth();
var year = today.getYear();
year <= 100 ? year += 1900 : 0;
var time = today.getHours();
if (time >= 12){
if (time > 12){time -= 12;}
today.getMinutes() < 10 ? time = time+":0"+today.getMinutes()+" PM" : time = time+":"+today.getMinutes()+" PM";
}else{
if (time == 0){time = 12;}
today.getMinutes() < 10 ? time = time+":0"+today.getMinutes()+" AM" : time = time+":"+today.getMinutes()+" AM";
}
return(this_month[month]+" "+day+", "+year);
}
function MakeArrayday(size) {
this.length = size;
for(var i = 1; i <= size; i++) {
this[i] = "";
}
return this;
}
function MakeArraymonth(size) {
this.length = size;
for(var i = 1; i <= size; i++) {
this[i] = "";
}
return this;
}
function funClock() {
if (!document.layers && !document.all)
return;
var runTime = new Date();
var hours = runTime.getHours();
var minutes = runTime.getMinutes();
var seconds = runTime.getSeconds();
var dn = "AM";
if (hours >= 12) {
dn = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}
if (minutes <= 9) {
minutes = "0" + minutes;
}
if (seconds <= 9) {
seconds = "0" + seconds;
}
movingtime = hours + ":" + minutes + ":" + seconds + " " + dn;
if (document.layers) {
document.layers.clock.document.write(movingtime);
document.layers.clock.document.close();
}
else if (document.all) {
clock.innerHTML = movingtime;
}
setTimeout("funClock()", 1000)
}
window.onload = funClock;
function pop_up(where, name, h, w){
options="toolbar=0, location=0, directories=0, status=0, menubar=0, resizable=0, width="+w+ " height="+h;
alert(options)
win = window.open(where, name, options);
}
now in the code, right about 5 lines up, where it has
window.onload = funClock;
there is no "}" below it, as if there is no need to signify the end of the function? it works in all the web pages, but is that ok?
also now, notice i took out the
<!--
//-->
<!-- Begin
// End -->
is that ok?
and is that ok how to write the whole code in the .js file?
other than that, that should do it...i just have to test it..will do by 2nite ...thanks!!
JaeSun
08-29-2002, 03:20 PM
Dr. Web!!
i was messing with the code, well, i implemented it, didnt touch nothing but the options for height, width, and URL in the link code, and when i click it, it brings up a message with this:
toolbar=0, location=0, directories=0, status=0, menubar=0, resizable=0, width=470 height=400
and then when you click ok, it brings up the window...
why does it do that?
here is a link to what im talking about:
http://www.satworld.biz/mobile/kvh/landmobile/
JaeSun
08-29-2002, 03:23 PM
nevermind, i fixed it...i took out the:
alert(options)
line in the code, and it doesnt ask for it anymore ....
Dr. Web
08-29-2002, 06:31 PM
whoops. forgot to take that out. Using alerts is a common way to display the variables you have created. Good to see that you were able to figure it out.
Cheers!
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.