There is a javascript on an external webpage that downloads a file. Can I pass a URL to a local script to download this automatically? Simply using http://webpage.com/javascript:theScript() doesn't seem to work, even when I type it into a browser. I'm very much novice in this space, be gentle! Thanks in advance.
This is too little info to tell you exactly how to do it (or even if it can be done at all). But what I can tell you:
http://webpage.com/javascript:theScript() cannot work the way you expect. The URL needs to locate a file, so if webpage.com was hosting a file named exactly `javascript:theScript()', then your browser would download the contents of this file and display it.
If the function you want to call resides in a javascript file, lets say, functions.js (URL: http://webpage.com/functions.js), then you can include this file in your own webpage and call functions from there.
This is where the special javascript: notation comes to place: It's a workaround to allow you to specify a javascript function in a regular <a href...> tag.
File: functions.js
function theFunction() {
alert ("Hey there!");
}
File: yourpage.html
<html>
<head>
<script type="text/javascript" src="http://webpage.com/functions.js">
</script>
</head>
<body onload="theFunction();">
Your text<br>
Click here
</body>
</html>
If this doesn't help you out, then you should post some code (ie. contents of the file containing the function to call, how you want to include the function in your page etc).
To include a script:
<script type="text/javascript" src="http://www.example.com/script.js"></script>
To execute code from the script you'll need a separate script tag:
<script type="text/javascript">
theScript(); //where theScript is a function defined in script.js
</script>
You should look at some tutorials for JavaScript. I recommend w3schools.
Related
As a begineer i am facing a problem, i followed an advise from several opinions to keep my HTML file clean, so i created a JS file and started migrating all scripts to a it, everything is Ok until i had to make this in it :
<script>
document.getElementById("mobile-toggle").addEventListener("click",function(){
document.getElementById("mobile-date").innerHTML = "Today is ...";});
</script>
When this script in is my HTML file it is run by the browser automatically but when i put it in the JS file, it simply don't work without being called with a function name, and that's what i want to avoid due to unobtrusive javascript recommendations, so my question is "is there a way to make a script from JS file to be run automatically without a call from HTML event ?
Thanks.
Include your script in the HTML with defer attribute. This will run the code when HTML is ready. You could place the script tag at the end before </body> as well, but I prefer having it on top.
<head>
<script src="/myscript.js" defer>
</head>
in myscript.js
(function() {
// your code goes here
})();
Wrapping this into a function gives your own scope and another wrap into parenthesis and () at the end will do the execution.
I have a JSP file that gives me a bunch of JS code that I need to put in my view page (particularly in my JSP file that is the view for the current URL. Using Spring MVC) . Is there a way for it?
I have already tried a bunch of suggestions from here: Including JSP code from another JSP file correctly and here https://coderanch.com/t/596663/frameworks/Calling-jsp-include-tag-javascript but none gives clear answer on it. When the view is rendered after hitting the controller it just gave a plain JS code in the window instead of embedding it as JS code.
<html>
<head> </head>
<script type="text/javascript">
<jsp:include page="./myJSPfiles/MyJSPfileforScript" />
</script>
</html>
As I said, it just gives plain JS code in the browser instead of embedding it. I am fairly new to JSPs in general. Any suggestions?
Use something like below line of code.
<script type="text/javascript" src="${pageContext.request.contextPath}/js/index.js"></script>
I am new in jQuery, so I need some help. I downloaded jQuery latest version from jquery.com.
I saved this as a text file as jquery.js. Then I keep my html code & jquery.js in the same folder.
When I run my code my jQuery code function doesn't work, but when I used jQuery from Google in my code it works very good
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
So please help me to get out my problem.
My code is given below:
<!DOCTYPE html>
<html>
<head>
<script src="javascript/jquerymin.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide("slow",function(){
alert("The paragraph is now hidden");
});
});
});
</script>
</head>
<body>
<button>Hide</button>
<p>This is a paragraph with little content.</p>
</body>
</html>
I guess you have a 404 error when loading javascript/jquerymin.js.
I save this as a text file as jquery.js. Then I keep my html code & jquery.js in same folder
This means that you have to change this:
<script src="javascript/jquerymin.js"></script>
into this:
<script src="jquery.js"></script>
Your application files will look like this:
site-root
├── index.html
└── jquery.js
if you are working on windows, make sure file extension isn't hidden (which is default behavior for windows platform). if the extension is hidden then Windows Operating system automatically adds extension to the file name you have given. for example, if you saved a file as "jquerymin.js" then the file has actually been saved as "jquerymin.js.txt".
Make sure you have the file jquerymin.js inside the javascript folder.
May be it is a typo,
You have used jquerymin.js for including jquery
Usually the jquery min file come with below name
jquery.min.js
So,
Check your file name whether it is jquerymin.js or jquery.min.js
and as your src="javascript/jquerymin.js" it should be in a javascript folder inside root
I'm creating a site, tested out on my local server and it worked fine, but once I moved it over ot my host anything to do with javascript/jquery on the page just refuses to work :/
http://www.neuromanga.com/mangaReader1.0.php?cid=1
could someone please help me out
You have <script> tags in your Javascript file. They need to be removed. I think this will fix your problem.
Please remove the <Script> and </Script> tags from your Mangacaller.js file.
if you use <Script src='...'/> you don't use the script tags in that source file.
The external script (Mangacaller.js) should not have the <script type="text/javascript"> and the corresponding </script> inside the external file. The external file should be all JavaScript without any HTML (it's just the stuff that would go between the <script> tags if you embedded the script inside the HTML instead of referencing it as an external script). The browser parses this entire external file as JavaScript, not HTML, so there should be no HTML within the Mangacaller.js file.
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.