linking code from a separate document to work on another page - javascript

I have taken script from a webpage document I have made and I have saved onto a notepad document with the extension .js.
I would like to now know how I can reference this .js file from the current page that I have created so that the script will run on that page without the actual code being there, just the reference link.

Something like this?
<script type="text/javascript" src="myfile.js" />

It must be a reference link. There are other techniques besides the standard, but they all rely on linkage. You can't beat the linkage. You can't stop the linkage. You mus succumb to the linkage.

That's not how the web works? You might be able to use some sort of developer tool to execute arbitrary javascript from a file when the page loads, but that would just be overriding the default way the web works.

you will need to do :
<script type="text/javascript" src="URI_TO_DIR/extension.js"></script>
see this page about embedding a javascript file
if script is on your computer, it will of course not be accessible on the web, for that you'd need a webserver or a webspace

You can include an external JavaScript using a script tag with a src attribute. For example, something like
<script type="text/javascript" src="javascriptfile.js"></script> should do what I think you're asking for.

Related

How to embed javascript file content into html page in Thymeleaf?

I need to embed javascript directly into html page generated by Thymeleaf
Something like:
<script th:include="../static/assets/generated/scripts.js"></script>
But this simple usage leads to SAXParseException...
Is there any easy way to switch off parsing of the th:included
content? Or any other way how to embed content of resource int the result page?
I don't think that is possible out of the box. You could probably write an extension that can do it. Or maybe there is an existing one, but I couldn't find one right now.
Does it have to to be a separate JavaScript file? Can't you put your JavaScript code into a fragment and include it like any other fragment?
NB: Including JavaScript into your HTML file like that is usually bad web design und may be a sign that you have bigger problems and you haven't structured your code well. Why do you think you need to do that? Why can't you refer to an external script file?
Thats not a Thymeleaf-Thing. It's classic html:
<script src="/assets/generated/scripts.js"></script>
In version 3.0, you can do it in this way
<script th:src="#{/webjars/jquery/dist/jquery.min.js}"></script>

JavaScript function not found in JSP

I have a few JSP pages, that include some JavaScript(jquery, jquery mobile and some javascript functions that I wrote).
When loading the pages and try to run my functions, I get in Firebug an error, that the function was not found. I have looked into the page source, and the function is there.
All the other jquery mobile functions work.
The only way to make my script work is to make a forced refresh(ctrl+f5).
Why is this happening? How can I fix it?
EDIT
It seems that a simple refresh would also work.
Here is the source code of the page:
http://pastebin.com/6sJnfPDQ
I have retagged your question to remove "Java" and "JSP", as this is irrelevant (server vs browser).
Once your JSP is rendered in the browser, please do look in the page source and see what happened to your tags.
make sure all of your js files are being loaded properly.
also make sure that your js files are being loaded in the proper order.
make sure that, where necessary, you're wrapping your JS in a document ready function of some type
also, I recommend that you add the type attribute to your script tags:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>

Adding Jquery to root folder in my server (localhost)

I am learning web development and I have no idea where to place jquery.js file on my localhost server. Do I just save it as such /var/www/jquery.js and then reference it with:
<script type="text/javascript" src="jquery.js">?
Will that work or is there a particular place that is called a root folder on localhost server that I don't know about...Sorry for how dumb this sounds..
Just put it anywhere, and include a <script> tag that embeds it, e.g. <script type="text/javascript" src="/some/location/in/my/webroot/jquery.js"></script>.
bob,
Its always good to maintain nice heirarcy structure
do as below
Create a separate folder for js , which is javascript and place all of them there
root/js/jquery.js
and you can include
/js/jquery.js
This should be good
either you can include from google cdn also , so that you can have better performance
http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
reference here
http://softwareas.com/google-jquery-cdn
You can place it anywhere you want as long as the includes contains the proper path.
If you place it here:
/www/ajax/jquery.js
Your includes would look like this:
<script type="text/javascript" src="/ajax/jquery.js"></script>
BTW: /www/ is your "web root"
EDIT:
I'd also place my JavaScript includes and scripts at the end of my body section...
Click here to read about script location and performance.

Include the prototype.js code directly in the html file

Is there a way to include the prototype.js code directly in the html file?
I do not want to have something like this:
<script type="text/javascript" src="/path/to/prototype.js"></script>
I want something like this:
<script type="text/javascript" >
// the code of prototype.js to be here
</script>
The context for what I want this is more complex. I just want to know now if this is possible or not.
P.S.I'm not very familiar with Prototype.
Thank you.
Yes it will work if you copy paste the entire content inside the script tags.
We do something similar with jQuery and our other JS and CSS files for our web app. They are all compressed and included inline when we run our deployment script.
It makes the page relatively heavy (300kb) but the main advantage is you get everything in a single http request. This makes the app look very fast.
The page is then cached, making next visits even more responsive.

Can a single javascript file be used by multiple html files?

I have a javascript file main.js and five html files 1.html,2.html,3.html,4.html,5.html
I want to access the javascript file main.as in all files .
I have used in all of the five but I'm not able to access it . Is it possible to share the .js file among the html files.
Plz reply,
Thanks in advance
Prashant Dubey
Yes, it is entirely possible. That's the point of being able to have a JS file instead of having to embed all the code in the HTML.
yes this is well possible. You merely have to include
<script type="text/javascript" src="main.js"></script>
in your HTML files, preferably near the bottom (for faster rendering). What have you done so far?
Yes. Totally possible.
Just reference it in all of the files e.g. by
<script type="text/javascript" src="Scripts/main.js"></script>
Yes, it is possible.
Probably there is something wrong with the way you access the javascript from your html. Show us the <script ...>...</script> part of your html.
Yes. Are you using the correct path to the main.js file in your html files?
Create separate javascript file with .js extension with all your function in it and
just include this javascript file in the head tag of all the html scripts u wanna use that in.
Like::
<html>
<head>
<script type="text/javascript" src="JavaScriptFilePath.js"></script>
</head>
<body>
<!-- use javascript -->
It can happen both ways..
a single html file can use multiple javascript file
2.a javascript file can be used in several html files.
In first case javascript file load can be conditional based on location, user preferences, time, age group, content restriction.
You can see good example when facebook loads its page. I loads number of javascritps.

Categories