I'm looking for a way to select and load a javascript from the html file arguments. The html file is called as follows:
OSM_Map.html?year=2017 or OSM_Map.html?year=2018
In the OSM_Map.html file there is the following code in the header:
<head>
.....
<script language="JavaScript" type="text/javascript" src="LatLonDB_2017.js"></script>
<script language="JavaScript" type="text/javascript" src="LatLonDB_2018.js"></script>
....
</head>
There is no problem to get the year argument from the argument list, but how can I load depending on the year argument just one of these .js files?
As somebody said, "yes, you can":
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/loadjs/3.5.5/loadjs.min.js"></script>
<script>
var myparameter = new URL(location.href).searchParams.get("year");
loadjs( "LatLonDB_" + myparameter +".js" );
</script>
</head>
<body>
<h1>Titulo</h1>
</body>
</html>
Been the URL something like: http://test.html?year=2018
BUT! not sure if this will work in every browser.... the "searchParams" is not universally compatible.
Thanks to #spencer.sm in this question How to get the value from the GET parameters?
and of course, loadJS function.
The LatLonDB_xxxx.js script still doesn't load. I'm not sure why not. Cannot you load a .js file from another directory than where the .html file is? Otherwise the load may be too late. Scripts following the LatLonDB_xxxx.js script use this DB.
The original code is like this:
<html>
<head>
...
<script language="JavaScript" type="text/javascript" src="../DataBases/LatLonDB_20xx.js"></script>
<script language="JavaScript" type="text/javascript" src="../DataBases/LatLonDB_utils.js"></script>
<script language="JavaScript" type="text/javascript" ...more scripts using the LatLonDB_20xx.js></script>
...
</head>
...
</html>
The intention is to replace the 20xx by the correct year: 2000, 2001, etc. There is no problem to get the correct year from the query parameters.
Related
I've founded this article but I don't know where to add this code:
$(document).ready(function(){
$(".showDescriptionTextbox").val($(".Product").val());//to initialize
$(".showDescriptionDiv").text($(".Product").val());//to initialize
$(".Product").change(function(){
$(".showDescriptionTextbox").val($(".Product").val());
$(".showDescriptionDiv").text($(".Product").val());
});
});
I've file jquery-1.11.3.min.js, jquery-1.10.2.min.js.
Should I edit one of both file with above code? Please give me a hint.
I want to execute query SQL by clicking the select option that I know it's just possible using jquery/ajax/js, but I'm still newbie.
I've file jquery-1.11.3.min.js, jquery-1.10.2.min.js. Both are jquery library file with different version. Only one should be included.
In your case the code snippet which you have shared can be included in a separate js file. Like this
nameOfYourJSFile.js
$(document).ready(function(){
$(".showDescriptionTextbox").val($(".Product").val());//to initialize
$(".showDescriptionDiv").text($(".Product").val());//to initialize
$(".Product").change(function(){
$(".showDescriptionTextbox").val($(".Product").val());
$(".showDescriptionDiv").text($(".Product").val());
});
});
Include the file in your main html file. Add the scripts near the bottom of the page.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Hello Plunker!</h1>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="nameOfYourJSScript.js"></script>
</body>
</html>
DEMO
If the file is included in the bottom , there is no need to use document.ready function
Add your code BELOW the <script src="jquery-1.11.3.min.js"></script>
So, JQuery will be loaded and can be used
I have this codes
//first.js
var site = {};
//and functions are declared like that
site.functionNames = function(){
//some codes
};
//second.js
$("#element").click(function(){
//more codes
site.function;
site.function();
console.log(site);
});
How can I access/call first.js functions from the second.js click event?
Tryed the ones above but all say site is undefined. Dont know if is the right way.
:Edit:
Files are already declared in order, second.js comes after some others.
<script src="first.js" type="text/javascript"></script>
<script src="second.js" type="text/javascript"></script>
As long you declare the javascript file in order you should be able to use it.
<head>
....
<script src="first.js" type="text/javascript"></script>
<script src="second.js" type="text/javascript"></script>
....
<head>
If you invert the order, wont work
<script src="second.js" type="text/javascript"></script>
<script src="first.js" type="text/javascript"></script>
A function cannot be called unless it was defined in the same file or one loaded before the attempt to call it.
A function cannot be called unless it is in the same or greater scope then the one trying to call it.
You declare function fn1 in first.js, and then in second you can just have fn1();
1.js :
function fn1 (){
alert();
}
2.js :
fn1();
index.html :
<script type="text/javascript" src="1.js"></script>
<script type="text/javascript" src="2.js"></script>
Source: Calling a javascript function in another js file
The browser is reading script tags in the order they are written, unless async is true. so just place your script tags the way you want and the browser will execute the second one after the first one.
Edit:
Maybe there's an error loading the js file. Check in the browser dev tools.
this is a working example
Test.html
<!DOCTYPE html>
<html class="html" lang="es-ES">
<head>
<script src="scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="tfirst.js" type="text/javascript"></script>
<script src="tsecond.js" type="text/javascript"></script>
</head>
<body>
<div class="fieldwrapper" name="field1" id="field1" />
First name: <input type="button" name="first" onclick="site.functionNames('html')"><br>
Last name: <input type="button" name="second" id="other"><br>
</div>
<body>
</html>
tfirst.js
var site = {};
//and functions are declared like that
site.functionNames = function(v){
alert("hello " + v);
};
tsecond.js
$(document).ready(function(){
$("#field1").on('click', '#other', function () {
site.functionNames('second.js');
console.log(site);
});
});
i would like to add javascript to an html page. I don't want to have those scripts inline on the home page however, I would like them to be in an external javascript file. How do I link to an external javascript file so those scripts are run on the page?
<html>
<head> <title>asc Computer Grass Roots pvt. ltd.</title>
<script src="jquery.js"></script>
<script> my jquesry scrips here
</script>
</head>
<body> somecodes </body>
</html>
Instead of
<script>my jquesry scrips here</script>
put
<script src="myscript.js"></script>
Of course you will have to create a file named 'myscript.js' and type javascript code in it.
For example, let say that you have all your javascript code in a file called 'functions.js', to link that javascript file to your html page just do it the same way you linked you jquery file.
<html>
<head> <title>asc Computer Grass Roots pvt. ltd.</title>
<script src="jquery.js"></script>
<script src="functions.js"></script> <!-- linking your file -->
<script>
/*my jquery scripts here */
</script>
</head>
<body> somecodes </body>
</html>
Keep in mind that if the code in your 'functions.js' file needs jQuery to work correctly you have to add 'jquery.js' before adding your custom file, just as it is in the example.
In the map of your website create a file called script.js
put your jquery script in there
replace:
<script>my jquery scrips here</script>
with:
<script src="script.js"></script>
just like you did with the jquery.js, next time search the web before asking questions on SO.
Example
if you google "link javascript file" the first link is your answer to the question (link above is the website).
I am currently working my way through David Sulc's excellent "Backbone.Marionette.js: A Gentle Introduction" and have come unstuck at modules. With the app as it currently stands I am trying to access an API in a module called 'contacts.js' from the index.html script but I get the following error when I try to run the app:
"Handler not found for 'contact: entities' "
I am able to hit the API directly from the console in Chrome and manually get the 'contact' information so I was thinking this was a loading sequence problem where the API might not be available by the time app.js loads, however the loading sequence is as follows:
<script type="text/javascript" src="assets/js/vendor/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="assets/js/vendor/json2.js"></script>
<script type="text/javascript" src="assets/js/vendor/underscore-min.js"></script>
<script type="text/javascript" src="assets/js/vendor/backbone-min.js"></script>
<script type="text/javascript" src="assets/js/vendor/backbone.marionette.min.js"></script>
then the local scripts as follows:
<script type="text/javascript" src="assets/js/app.js"></script>
<script type="text/javascript" src="assets/js/entities/contact.js"></script>
and a script tag below the last two script tags directly runs the following code:
<script type="text/javascript">
... some view code
ContactManager.on("initialize:after", function(){
var contacts = ContactManager.request("contact: entities");
... some more code
</script>
the line beginning var contacts = ..... is the one giving me the error. Any help appreciated
I am as certain as I can be that I have scripted this exactly as per the book.
You have a space in your event name:
<script type="text/javascript">
... some view code
ContactManager.on("initialize:after", function(){
// Remove the space in contact: entities -->
var contacts = ContactManager.request("contact: entities");
... some more code
</script>
I Have usercontrol and i registered the following javascript method in code behind :
changeSelectedMenu(controlID);
the previous method is declared in JScript.js file so i registered it also .
but the following exception has been thrown :
Microsoft JScript runtime error: Object expected
The result page is :
<script src="~/Scripts/JQuery.js" type="text/javascript"></script>
<script src="~/Scripts/JScript.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
changeSelectedMenu('ctl00_ctl00_cntMain_procedureContentHolder_procedureMenu_appointmentsLink');//]]>
</script>
Is There anyone can help me to workaround this issue???
These aren't valid paths to your JavaScript:
<script src="~/Scripts/JQuery.js" type="text/javascript"></script>
<script src="~/Scripts/JScript.js" type="text/javascript"></script>
They need to be relative to the page or to the site root, for example:
<script src="/Scripts/JQuery.js" type="text/javascript"></script>
<script src="/Scripts/JScript.js" type="text/javascript"></script>
While ~/ works for resolving a path on the server-side, the browser doesn't know how to handle this. There are some other work-arounds in this question for making it work and still being app-relative.