Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening External javscript function through a link 1

Status
Not open for further replies.

DBSSP

Programmer
Apr 1, 2002
327
US
I have coded a frameless popup window as a function but I would like to make the code an external file. I have created the external file but can't figure out how to call the function from my html page with a link or button. I've been fighting with this for for a day now! [mad] Any ideas on how I can do this? [idea] Jay [infinity]
"If the words up and down were reversed, would you trip and fall or trip and fly?"
 
eh?
<script language=&quot;javascript&quot; src=&quot;externalFile.js&quot;></script>

<input type=&quot;button&quot; value=&quot;Click Me&quot; onclick=&quot;functionName();&quot;/>

<a href=&quot;#&quot; onclick=&quot;functionName();&quot;>Click Me</a>
=========================================================
if (!succeed) try();
-jeff
 
I've tried both of those and keep getting an error &quot;Object Expected&quot;. Same thing that happening to me before. Any ideas why? I do appreciate the input though! Jay [infinity]
&quot;If the words up and down were reversed, would you trip and fall or trip and fly?&quot;
 
&quot;Object expected&quot; means it cannot find the function you're calling.

let me see if I understand: you have a function in an external js include (.js file), and you want to be able to access that function from a pop-up window?

=========================================================
if (!succeed) try();
-jeff
 
Close!
I'm trying to call the external function from an html document. The function then opens a popup window. Does this make a little better sense?

It works, of course, if I include the whole script in the html document that calls the function, but I want it external (less clutter, easier maintanance). Jay [infinity]
&quot;If the words up and down were reversed, would you trip and fall or trip and fly?&quot;
 
this is the proper syntax for including an external js file:
[tt]
<html>
<head>

<script language=&quot;javascript&quot; src=&quot;myFile.js&quot;></script>

</head>
<body>
My page contents here...
</body>
</html>

[/tt]

make sure NOT to include the <script> & </script> tags inside the myFile.js file!

myFile.js:[tt]
// beginning of file

function myFunction() {

}

// end of file

[/tt]

=========================================================
if (!succeed) try();
-jeff
 
It's not working at all. I still get the same thing. let me show you the code so that you can get a better understanding of how it's suppossed to work. Maybe the remedy is right in front of me, I'm just not seeing it! lol


Begin Code
***************************
// set the popup window width and height

var windowW=583 // wide
var windowH=330 // high
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Set the screen position where the popup should appear.
// See the following settings.
//***************************************************************
//Programmer Specifies Window Position *
// Un-comment Set 1 & Comment Set 2 out*
//Automatically center on screen *
// Un-comment Set 2 & Comment Set 1 out
//**********************************************************
//---------------
//Set 1 |
//---------------
//var windowX = 260 // from left
//var windowY = 100 // from top
//---------------
//Set 2 |
//---------------
var windowX = (screen.width/2)-(windowW/2);
var windowY = (screen.height/2)-(windowH/2);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// set the url of the page to show in the popup

var urlPop = &quot;The web site.htm&quot;

// set the title of the page

var title = &quot;Calculator&quot;

// set this to true if the popup should close
// upon leaving the launching page; else, false

var autoclose = true

// ============================
// do not edit below this line
// ============================

s = &quot;width=&quot;+windowW+&quot;,height=&quot;+windowH;
var beIE = document.all?true:false

function openFrameless(){
if (beIE){
JMS = window.open(&quot;&quot;,&quot;popFrameless&quot;,&quot;fullscreen,&quot;+s)
JMS.blur()
window.focus()
JMS.resizeTo(windowW,windowH)
JMS.moveTo(windowX,windowY)
var frameString=&quot;&quot;+
&quot;<html>&quot;+
&quot;<head>&quot;+
&quot;<title>&quot;+title+&quot;</title>&quot;+
&quot;</head>&quot;+
&quot;<frameset rows='*,0' framespacing=0 border=0 frameborder=0>&quot;+
&quot;<frame name='top' src='&quot;+urlPop+&quot;' scrolling=auto>&quot;+
&quot;<frame name='bottom' src='about:blank' scrolling='no'>&quot;+
&quot;</frameset>&quot;+
&quot;</html>&quot;
JMS.document.open();
JMS.document.write(frameString)
JMS.document.close()
} else {
JMS=window.open(urlPop,&quot;popFrameless&quot;,&quot;scrollbars,&quot;+s)
JMS.blur()
window.focus()
JMS.resizeTo(windowW,windowH)
JMS.moveTo(windowX,windowY)
}
JMS.focus()
if (autoclose){
window.onunload = function(){JMS.close()}
}
}
**********************************
End Code

So there's my java script file. I'll keep trying things, thanks for your help so far though! Anyone who wants to use this code, please feel free to do so! Jay [infinity]
&quot;If the words up and down were reversed, would you trip and fall or trip and fly?&quot;
 
i made two test pages:

1. has the above script directly on it
2. uses the above script in an external include.

both work properly.

look at the line that it says &quot;Object expected&quot; at ... make sure you're spelling the function correctly. javascript is case sensitive.

=========================================================
if (!succeed) try();
-jeff
 
<html>
<head>
<title>Test Page</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<script src=&quot;/JavaScripts/TestPop3calc.js&quot;></script>
</head>
<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>

<a href=&quot;#&quot; onclick=&quot;openFrameless();&quot;>Click Me</a>
</body>
</html>

This is how I have it in the calling html page, and I'm still getting an error. Arrrgggg! This is very frustrating and I know I'm missing something extremely dumb. I also tried the full path for the javascript. Here is what I get.

Line 9, char 1 (referring to the above link), Object Expected, code 0.... Jay [infinity]
&quot;If the words up and down were reversed, would you trip and fall or trip and fly?&quot;
 
<script src=&quot;/JavaScripts/TestPop3calc.js&quot;></script>

beginning your src URI with the forward slash &quot;/&quot; instructs the browser to start in the ROOT of your directory...so if your page is running from &quot;c:/folder/web/page.html&quot;, then the above include will start in C:
if your include actually resides in c:/folder/web/JavaScripts/ then remove the leading forward slash.

hope this helps
=========================================================
if (!succeed) try();
-jeff
 
Your not gonna believe this, but I tried the full path and I'm still getting the same error...But I tried something else, I tried uploading the file to my webserver (duh!) and used an http reference and it works! I knew it was something dumb I was doing! Thank you for your help, it was greatly appreciated! Star For you! Jay [infinity]
&quot;If the words up and down were reversed, would you trip and fall or trip and fly?&quot;
 
I had the same problem as DBSSP and I did what he did.

I uploaded my files to the server and bingo...

I know why I couldn't src my .js file on my local windows machine. I put it directly in the same directory as my HTML page.

!!! I also removed the <SCRIPT></SCRIPT> tags and just used the bare functions in my scripts.js file. I don't know if this was important, but I think it makes sense.

Ken954
 
Ken954,

yes, you should not include <script></script> in the actual .js include.

what version windows are you running?
=========================================================
while (!succeed) try();
-jeff
 
I am using windows 2000, but I don't have a problem any longer... I work locally with the scripts on the page and then I cut the scripts and save them to a .js file and upload the works to the server.

I strip the script tags on the .js file.

Everything works fine then.

Thanks for your help... I wouldn't have know to strip the script tags.

Ken
 
DBSSP,

Just on a lark, try putting a copy of your .js file in the same directory as your calling HTML file. Then -- again, temporarily -- change the line in your HTML to read

Code:
<script language=&quot;javascript&quot; src=&quot;TestPop3calc.js&quot;></script>

Using the full http might have done the trick, but could kill your portability. If you can get it to run this way, then your trouble was with the path as previously described. Fixing a bad path is always a good idea.

Cheers,
[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top