/    Sign up×
Community /Pin to ProfileBookmark

can’t use functions from included .js file

I have been building a rather complex site, and have several files of functions that are included by the frameset page, in the <head>, with:

[code]
<script language=”javascript” src=”sourcefile1.js”>function descs</script>
<script language=”javascript” src=”sourcefile2.js”>more desc</script>
[/code]

The files that contain simple (straight? only?) functions load fine, and work fine. I recently tried to add a file with some object-making functions. I wrapped the code in HTML and script tags to test them stand-alone, and they worked fine. ‘new ObjectWhatever()’ created a new object, the methods all worked, etc. So, I removed the markup and script tags, saved it with a .js extension, and included it, just like all the other source files:

[code]
<script language=”javascript” src=”newfile.js”>comments</script>
[/code]

None of the object functions are accessible in the html pages. I am on Mac OS 10.4, and have tried both Safari and Firefox. The file seems to /load/ fine, the alerts I added verify that, but the code .. just isn’t accessible, apparently.

I did add a non-constructor function to the misbehaving file, and can access it with ‘top.func()’, not func(), but ‘new top.constructor()’ doesn’t work, either.

This is driving me mad. If anyone can point out what I’m doing wrong, I’d be deeply grateful.

If you need the actual code, let me know.

Thanks in advance, just for reading!
cct

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@bals28mjkSep 13.2007 — Hi ctt,

The first thing I would check for is conflicting variable names or functions. If you already carefully scanned your script, do it again. It's one of the hardest errors to find. Other than that, I can't offer any more advice without more code or info.
Copy linkTweet thisAlerts:
@cctauthorSep 14.2007 — Addendum:

Part of the problem seems to be that the functions won't work through multiply-nested framesets. For 'normal' functions, top.func() will work, but that doesn't seem to work for constructors, so, bleh. I have to remember to load the objects file into any page I want to use them in. Actually, I /think/ I only have to load the file into any page that *creates* an object.. the object itself (if attached to the top level) should be available everywhere? If anyone knows otherwise, let me know. ?

Also, I did not find any namespace problems. I /did/ find some small bugs .. some seem to work anyway, /if/ the code testing them is in the same file as the object functions, but not if the object functions are included from an external file. Weird, but apparently true. At any rate, I've been tracking those down, so I'm tentatively calling this one resolved. There's a lot of code to adapt, and it will take me time to do anything approaching reasonably comprehensive testing, but when I do I'll mark the thread resolved. Assuming it actually is, at that point.

Anyway! Thanks for the effort, I appreciate it.
Copy linkTweet thisAlerts:
@bals28mjkSep 14.2007 — Frames can be a hassle to work with. However, if you include the frames you want inside the frameset everything is accessable on them: objects, functions, variables, and all ? If you're using a child of a frame and want to grab a function from another child you can either reference with an array top.[i]frames[1][/i].[i]func()[/i] or call the name of the frame top.[i]frameName[/i].[i]func()[/i]. Also, top references the top most frame (or highest) if your children have children and so on. To reference only the parent of a child use parent instead of top.
Copy linkTweet thisAlerts:
@cctauthorSep 14.2007 — Yes, that was how I understood it when I embarked on the whole object thing. The difficulty, based on how my pages have been behaving, is that any frameset outside the one containing the frame you're working with are not automatically searched for functions -- you have to explicitly specify top.func() or whatever. And even that would be fine, except, apparently, you [I]can't[/I] do that with constructors. Hence the problem. If I'm missing something there, I'd be happy to hear about it. ?

Incidentally, for the morbidly curious, the frame structure I'm currently dealing with:
<i>
</i>&lt;frameset&gt; top-level, never-goes-away, containing frameset - tried loading constructors here for use below
&lt;frame&gt;permanent header stuff&lt;/frame&gt;
&lt;frame&gt;everything else
&lt;frameset&gt;divide frame into several areas <br/>
&lt;frame&gt;navigation links&lt;/frame&gt;
&lt;frame&gt;actual stuff - tried to [i]use[/i] constructors here, didn't work&lt;/frame&gt;
&lt;frame&gt;sometimes another frame&lt;/frame&gt;
&lt;/frameset&gt;
&lt;/frame&gt;
&lt;/frameset&gt;


I think I got that all right..

Thanks!

cct
Copy linkTweet thisAlerts:
@bals28mjkSep 14.2007 — apparently, you can't do that with constructors[/QUOTE]Yes you can. A constructor is just a different name for what a certain function does, the functionality is the same. Though, I probably should have specified the child to parent parent.[i]func()[/i] and the top.[i]func()[/i] in my explanation. The frames are not the problem, relook your JavaScript code.
Copy linkTweet thisAlerts:
@cctauthorSep 15.2007 — Thanks, that would certainly be useful, but I [i]tried[/i] that, and it didn't work...

... though, when I tried it, there may still have been some of those weird 'I work in a stand-alone file but not when I'm included' bugs floating around. So, I'll try it again with something I know works when included. Cool!!

thanks!
Copy linkTweet thisAlerts:
@bals28mjkSep 15.2007 — 'I work in a stand-alone file but not when I'm included'[/QUOTE]As metioned earlier, your problem more than likely is coming from conflicting variable and/or function names.
×

Success!

Help @cct 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.18,
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,
)...