check if prototype has been included?

0 views
Skip to first unread message

bdee1

unread,
Nov 27, 2007, 11:52:04 AM11/27/07
to Ruby on Rails: Spinoffs
i am using a hosted cms that will automatically include prototype.js
but only if certain elements of the CMS are being used on the page.
if those elements are not being used on the page then prototype is not
included.

this is a pain because i want to write some of my own prototype
scripts to use on the page but if prototype is not included i
obviously get errors with my scripts.

so with that said, i was thinking that there must be a way to include
a javascript that check to see if prototype has been included already,
and inf it hasn't been included - include it.

does anyone have any idea how i could accomplish this?

Ryan Gahl

unread,
Nov 27, 2007, 11:56:48 AM11/27/07
to rubyonrail...@googlegroups.com
You could just do a simple check to see if Prototype has been defined...

if (!Prototype)
    includePrototype()


Where I'll leave the implementation of includePrototype() for you to Google ("lazy loading javascript files")...


Nth Penguin, LLC
http://www.nthpenguin.com
--
Architect
WebWidgetry.com / MashupStudio.com
Future Home of the World's First Complete Web Platform
--
Inquire: 1-262-951-6727
Blog: http://www.someElement.com
LinkedIn Profile: http://www.linkedin.com/in/ryangahl

Gregory Seidman

unread,
Nov 27, 2007, 11:59:30 AM11/27/07
to rubyonrail...@googlegroups.com

<script type="text/javascript">
if (!(window.Prototype && window.Prototype.Version)) {
document.write('<scrip' +
't type="text/javascript" src="javascripts/prototype.js"></scrip' +
't>';
}
</script>

--Greg

bdee1

unread,
Nov 27, 2007, 1:58:00 PM11/27/07
to Ruby on Rails: Spinoffs
i keep getting an "unterminated string literal" error on the following
line:

document.write('<script type="text/javascript" src="/Websites/ndbg/
templates/NDBG/scripts/prototype.js"></script>');

not sure why. any thoughts?

On Nov 27, 11:59 am, Gregory Seidman <gsslist
> --Greg- Hide quoted text -
>
> - Show quoted text -

Brian Williams

unread,
Nov 27, 2007, 1:59:44 PM11/27/07
to rubyonrail...@googlegroups.com
did you wrap the text or did the email client wrap the text?
as in

is there actually a line break in your code?

document.write('<script type="text/javascript" src="/Websites/ndbg/
templates/NDBG/scripts/prototype.js"></script>');

bdee1

unread,
Nov 27, 2007, 2:01:39 PM11/27/07
to Ruby on Rails: Spinoffs
that was the email client - in my page there is no break in the text.
also i notice that on my page, a '); } is displayed so it looks like
it is dying right before the closing single quote.

On Nov 27, 1:59 pm, "Brian Williams" <brianw1...@gmail.com> wrote:
> did you wrap the text or did the email client wrap the text?
> as in
>
> is there actually a line break in your code?
>
> document.write('<script type="text/javascript" src="/Websites/ndbg/
> templates/NDBG/scripts/prototype.js"></script>');
>
> > > - Show quoted text -- Hide quoted text -

Elden

unread,
Nov 27, 2007, 2:17:10 PM11/27/07
to rubyonrail...@googlegroups.com
i don't know but it works


escaping the slash of closing script tag O.o


document.write('<script src=\"me.js\" type=\"text/javascript\"><\/script>');

Gregory Seidman

unread,
Nov 27, 2007, 2:44:18 PM11/27/07
to rubyonrail...@googlegroups.com
On Tue, Nov 27, 2007 at 01:17:10PM -0600, Elden wrote:
> i don't know but it works
>
> escaping the slash of closing script tag O.o
>
> document.write('<script src=\"me.js\" type=\"text/javascript\"><\/script>');

If you had copied my script verbatim (except changing the src URL), it
would have worked. Did you not notice that I was deliberately breaking up
the <script and </script tags? The unterminated string literal error is
because the browser sees </script> and takes it to mean the end of the
current script tag, even though it is in a string literal. Escaping the /
doesn't so much escape it as keep it from looking like a proper end tag.

--Greg

bdee1

unread,
Nov 27, 2007, 2:48:20 PM11/27/07
to Ruby on Rails: Spinoffs

oh - ok that makes sense - i had noticed that you broke up the script
tags but had no idea why. now i get it.

one more question though, now that this is working...

how could i do the same thing for scriptaculous - check to see if it
is included and if not, include it.
On Nov 27, 2:44 pm, Gregory Seidman <gsslist
> > > > - Show quoted text -- Hide quoted text -

bdee1

unread,
Nov 27, 2007, 3:36:36 PM11/27/07
to Ruby on Rails: Spinoffs
any ideas on this?
right now i am particularly looking for the existance of effects.js
but it woudl be cool to have a test for each of the available
libraries (builder.js, controls.js, dragdrop.js, effects.js,
slider.js, sound.js)

Wizz

unread,
Nov 28, 2007, 3:31:23 AM11/28/07
to Ruby on Rails: Spinoffs
If the entire CMS is used without frames, you could always iterate
through all the script elements and check if the ones you need are
there.

$$('script').each(function(s) {
var scr = s.readAttribute('src');
// do something with src
});

I think you can also just include the scripts you need, where you need
them... I'm not sure about this, but if there is a duplicate script it
will not load it again.

Wizz

bdee1

unread,
Nov 28, 2007, 9:08:05 AM11/28/07
to Ruby on Rails: Spinoffs
will that code check for script tags in the whole document (head and
body) or just the body? just curious because for soem reason my gut
is telling me that it would just check within the body but i could be
completely wrong.
Reply all
Reply to author
Forward
0 new messages