Library call in javascript - javascript

Is it possible to replace library call in HTML by library call in JS ?
For example here, is it possible to delete <script type="text/javascript" src="../node_modules/projet/libs/libraryexample.js"></script> from HTML and call it JS file instead ?
<head>
<title>pagetest</title>
<script type="text/javascript" src="../node_modules/projet/libs/libraryexample.js"></script>
<script lang="javascript" src="./myjs.js"></script>
</head>
and the JS file:
var myObject = new ObjectFromMyLibraryExample();
var myPart = myObject.addText("hello");
I have tried import "libraryexample" but it didn't work and I don't know how to do.

Related

How do I load a selectable javascript in html header

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.

Call function from variable in another js file

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);
});
});

How can I access other javascript var

I have an app and different javascript files.
From HTML I want to be able to access var from those different javascript files.
How should I do that??
EXAMPLE JAVASCRIPT:
var one = {
att1: 'myString1',
att2: 'myString2'
...
};
var two = {
att1: 'myString3',
att2: 'myString4',
...
};
EXAMPLE HTML:
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" charset="utf-8" src="js/idiomas.js"></script>
<script type="text/javascript" src="jqueryMobile/jquery-1.11.0.js"/></script>
......
EDIT:
Sorry, missed one part. This is where I try to access the data:
<script type="text/javascript">
var atrib1 = document.getElementById("idAtr1");
var atrib4 = document.getElementById("idAtr4");
.....
atrib1.innerHTML = one.att1;
atrib4.innerHTML = two.att2;
....
</script>
You have to maintain hierarchy. For example.
<script src="file1.js"></script>
<script src="file2.js"></script>
then you can able to access file1.js variables on file2.js but can't access file2.js on file1.js. Rules is javascript read files sequentially. So, if you try to access function/variables/object before they are available will get error.
Otherwise, If you want to access both files variable, you may try followings:
<script src="file1.js"></script>
<script src="file2.js"></script>
<!-- import your js files that contain those variable -->
<script>
var atrib1 = document.getElementById("idAtr1");
var atrib4 = document.getElementById("idAtr4");
//import your js files above and then call it.
atrib1.innerHTML = one.att1;
atrib4.innerHTML = two.att2;
</script>
If you have still doubt. Mention that on comment. We'll try to help :)
Simply add those js files in your HTML, and access those variables normally.
<!-- Your script containing the variables -->
<script type="text/javascript" charset="utf-8" src="js/idiomas.js"></script>
...
<script type="text/javascript">
console.log(one); // Access the variable normally
</script>

Simple comet application "org is not defined"

I am trying to update an old cometd javascript wrapper and test client (was 1.3.x) that I have to the newer comet 2.5.1 javascript implementation. I have all of the dependencies and the browser can find them all, yet I am getting errors in Firebug's console (see below)
The head of my HTML is as below:
<head>
<title>CometD Tester</title>
<link rel="stylesheet" type="text/css"href="style/style.css" />
<script type="text/javascript" src="org/cometd/Cometd.js"></script>
<script type="text/javascript" src="org/cometd/AckExtension.js"></script>
<script type="text/javascript" src="org/cometd/ReloadExtension.js"></script>
<script type="text/javascript" src="jquery/jquery-1.9.0.js"></script>
<script type="text/javascript" src="jquery/jquery.cookie.js"></script>
<script type="text/javascript" src="jquery/jquery.cometd.js"></script>
<script type="text/javascript" src="jquery/jquery.cometd-reload.js"></script>
<script type="text/javascript" src="js/myCometd.js"></script>
</head>
All of these are found by the browser. Looking at Cometd.js I see the following:
org.cometd.Cometd = function(name)
{
....
}
So is that not defining org? Note that none of the errors in the Console are from Cometd.js. Otherwise I see no other definition of "org.cometd". I would really appreciate it if anyone can help me out. I am using Tomcat 7 and below is the dir structure:
Thanks.
UPDATE - Further testing
I reduced the header to:
<head>
<title>CometD Tester</title>
<link rel="stylesheet" type="text/css"href="style/style.css" />
<script type="text/javascript" src="org/cometd/Cometd.js"></script>
</head>
And removed ALL JS from the index.html. The only JS now included is the Cometd.js from the comet.org. There is still the same error... coming from the very first line in that script:
org.cometd.Cometd = function(name)
Not sure what I have missed here.
EDIT - Add jquery.cometd-reload.js
This is the contents of the file. It looks like it is "re-binding" functionality from the cometd library to use the jquery one instead (?). I'm not up to speed enough in JS to debug this (I'm a C++ dev really).
(function($)
{
function bind(org_cometd, cookie, ReloadExtension, cometd)
{
// Remap cometd COOKIE functions to jquery cookie functions
// Avoid to set to undefined if the jquery cookie plugin is not present
if (cookie)
{
org_cometd.COOKIE.set = cookie;
org_cometd.COOKIE.get = cookie;
}
var result = new ReloadExtension();
cometd.registerExtension('reload', result);
return result;
}
if (typeof define === 'function' && define.amd)
{
define(['org/cometd', 'jquery.cookie', 'org/cometd/ReloadExtension', 'jquery.cometd'], bind);
}
else
{
bind(org.cometd, $.cookie, org.cometd.ReloadExtension, $.cometd);
}
})(jQuery);
So the problem was that I misunderstood the project layout from the Comet.org site. I should have followed the direction posted at cometd primer for non-maven setups a lot more closely. Basically when you are setting up the project you download the distribution, and then you need to take the code from the war files bundled inside the tarball.
SO, once you have extracted the tarball...
Take the org folder from cometd-javascript-common-2.5.1.war (located in \cometd-2.5.1\cometd-javascript\jquery\target) or cometd-javascript-jquery-2.5.1.war (located in \cometd-2.5.1\cometd-javascript\common\target)
Take the jquery folder from cometd-javascript-jquery-2.5.1.war
The org namespace definition was in the file org/cometd.js which I did not have before, as I wrongly assumed that it had been replace by the org/cometd/Cometd.js file. The namespaces org and comet are defined as below starting on line 17 of that file:
// Namespaces for the cometd implementation
this.org = this.org || {};
org.cometd = {};
org.cometd.JSON = {};
The functions are working correctly now.
Try loading jQuery before any of the other JavaScript files -
<head>
<title>CometD Tester</title>
<link rel="stylesheet" type="text/css"href="style/style.css" />
<script type="text/javascript" src="jquery/jquery-1.9.0.js"></script> <!-- load first -->
<script type="text/javascript" src="org/cometd/Cometd.js"></script>
<script type="text/javascript" src="org/cometd/AckExtension.js"></script>
<script type="text/javascript" src="org/cometd/ReloadExtension.js"></script>
<script type="text/javascript" src="jquery/jquery.cookie.js"></script>
<script type="text/javascript" src="jquery/jquery.cometd.js"></script>
<script type="text/javascript" src="jquery/jquery.cometd-reload.js"></script>
<script type="text/javascript" src="js/myCometd.js"></script>
</head>

Import javascript into html on play 2.0 framework

I'm having problems including local javascript files into my html that is on the play framework. The paths are correct and I even tried including the javascript file in the same directory. However, imports from the web (the main libraries i'm using) work just fine.
#(execId: String)
<html>
<head>
<title>Timeline</title>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
<script type="text/javascript"
src="http://code.jquery.com/jquery-latest.js"></script>
<script type = "text/javascript" src = "../../public/javascripts/profilesJS/stack.js"> </script>
</head>
<body>
<input id="profiles" type="button" value="Profiles" />
<script type="text/javascript">
alert(tester());
</script>
</body>
</html>
the javascript file simply looks likes this
function tester(){
return "test";
}
And the error i get is:
tester is not defined
at the line with the alert
According to the assets documentation (and routing in general) you need to use the reverse routing in your template:
<script type="text/javascript" src='#routes.Assets.at("javascripts/profilesJS/stack.js")'></script>
it builds the correct src path to your /public/javascripts/profilesJS/stack.js file (by default routing config it will be /assets/javascripts/profilesJS/stack.js)

Categories