Access a Function From Another File - javascript

I have two file:
html2canvas.js
function html2canvas(){
}
myid_print.js
(function ($) {
$(document).ready(function() {
//I want to call html2canvas function here
html2canvas();
});
})(jQuery);
I already included both files in my html but after running the code above, the console shows an error saying:
Uncaught ReferenceError: html2canvas is not defined
How will I call the function from inside my second file?

Try implementing your Client side code as :
<head>
....
<script src="html2canvas.js" type="text/javascript"></script>
<script src="myid_print.js" type="text/javascript"></script>
....
<head>
<body>
...
<script type="text/javascript">
function_from_myid_print();
</script>
...
</body>
Inside which you can call html2canvas();
This will surely help you.
For more details refer links:
https://stackoverflow.com/a/3809896/4763053 and https://stackoverflow.com/a/25963012/4763053

Try put your JS script at the bottom of your HTML page.

Related

kongregate not defined error javascript

I have included the required files in the head as it says in the docs
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js'></script>
<script src='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></script>
And then right above my scripts i included the script that supposed to define the variable
<script>
kongregateAPI.loadAPI(function(){
window.kongregate = kongregateAPI.getAPI();
});
</script>
But in the console I am still getting this error
Uncaught ReferenceError: kongregate is not defined
You said:
And then right above my scripts i included
Does it mean that your code looks like this?
<script>
kongregateAPI.loadAPI(function(){
window.kongregate = kongregateAPI.getAPI();
});
</script>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js'></script>
<script src='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></script>
If so, you should call kongregateAPI functions after you loaded api js file:
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js'></script>
<script src='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></script>
<script>
kongregateAPI.loadAPI(function(){
window.kongregate = kongregateAPI.getAPI();
});
</script>
I've tried it, everything works fine.

Using JavaScript in another JavaScript file

I have two javascript files and one html page. i want to include a js file to another js file but i dont know how to include js file to another one.
anyone can please help me?
following is my code
html code:
<html lang="en">
<head>
<script src="main.js" ></script>
</head>
<body onload="show()">
</body> </head>
</html>
following are the javascript files:
1.js
var x=10;
function show()
{
setvalue(x)
}
2.js
var D=;
function setvalue(x)
{
D = x;
alert(D);
}
if it's just a matter of accessing the functions and objects in the first javascript file just do something like this:
<script src="main1.js"></script>
<script src="main2.js"></script>
this way any function in main1.js will be available in main2.js

usebility functions from different jquery file

I have couple functions that I would like to separate from the main jquery file. However if I just put them in different file I will receive error when I will try to call it. - TypeError: $ is not a function
Well it a simple error that solved by put in all function in
jQuery(document).ready(function($) { });
Well but then as soon main jquery file will try to call the function it will gave the error that function is not defined.
So I find out that function defined within one $(document).ready block cannot be called from another $(document).ready block,
therefore my question is how to call function from different file.
The problem with functions is that I can't use it without jQuery(document).ready block, but as soon I put ready block function is not visible for main jquery file .
Here is the test -
https://stackoverflow.com/a/1327766/4849611
so for many comment here is my links
<script type="text/javascript" src="js/jquery.js"> </script>
<script type="text/javascript" src="js/function.js"></script> <!--file with functions-->
<script type="text/javascript" src="js/main.js"> </script> <!-- file which try to call functions--->
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="js/jquery.webcam.js"> </script>
I will specified the problem, since people don't understand...
simple function - as example in function.js file
function dialog(){
$("#message").dialog({
modal: true,
buttons: {
Ok: function() {
$( this ).dialog( "close" );
}
}
});
}
Main file -main.js
jQuery(document).ready(function($) {
$(document).on("click", "#take_one", function(e){
e.preventDefault;
dialog();
return false;
});
});
Include the other file in a script tag on the page.
<script src="path/to/your/script.js"></script>

externally loaded javascript won't execute

I'm having trouble running javascript from an external file. Here's where it's included in the html:
<div id="article-author-list" class="article-author-list">
<#list authorGroups as authorGroupItem>
<#authorGroup item=authorGroupItem/>
</#list>
<script type="text/javascript">alert('Hello??');</script>
<script type="text/javascript" src="/js/article/truncateAuthors.js"> </script>
</div>
Whereas here's truncateAuthors.js:
alert('Found the script!!!');
$(window).load(function () {
alert('Found the script.');
});
$(document).ready(function () {
alert('Document is ready');
});
$(function(){
alert('Running the script');
});
When the html is loaded, the only alert is 'Hello??' from the inline script. How can I get the external file to execute?
Before call your javascript please add this
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
to your file it will work fine. this is jquery library file that we have to use when we are writting code in jquery
I found it! The project was using requireJS and I needed to add it to the declaration. Thanks for all your answers :)

How to call javascript file in html

I am trying to call innerHtml in JavaScript its working in the same file but not in the separate JavaScript JS file.
my working code is
<script type="text/javascript">
function my()
document.getElementById("abc").innerHTML="hello";
}
</script>
<div id="abc" onmouseover ="my()"> hi hw ru </div>
But if I invoke this method in separate JavaScript file its not working even I am giving the source path of the JavaScript file like
<script type="text/javascript" src="js/framemrq.js">
Missing the function keyword
<script type="text/javascript">
function my(){
// Your code here
}
</script>
please define your my function correctly like this:
function my() {
document.getElementById("abc").innerHTML="hello";
}

Categories

Resources