I have to load two javascript files from the same company. These javascript files are different and contain different functions, but they use the same namespace (the company's name). I have both files included in my php file and when I try to create an object specified in one of the external js in my internal js code it searches for the object in the other js file so I get an error saying the function does not exist. When I comment out the other external file include, my code works. How do I force my internal js to look for the function in a specific external js file?
Can you alias the other function to another variable?
e.g:
<script src='functions1.js'></script>
<script>
var function1 = myCompany.someFunction
</script>
<script src='functions2.js'></script>
<script>
var function2 = myCompany.someFunction
</script>
Related
I am using ASP.NET MVC and I have a javascript file in ~Scripts/bundle/build.js. The file is generated with webpack, which renders a bunch of ES6 javascript files in Scripts. I am loading bundle.js first and then calling the function in a different script like this:
// #Scripts.Render("~/bundles/Webpack")
<script src="~/Scripts/build/bundle.js"></script>
<script>
connectToVnc();
</script>
(first line is commented, I have tried loading the script using bundles in BundleConfig.cs).
I get the error connectToVnc is not defined, even though it is defined in bundle.js.
What am I doing wrong?
Is it possible to download a script file using the script tag like this
<script src="http://webserver.com/script.js"></script>
and then be able to read the loaded file content with JavaScript, like this for example:
var scriptContent = window.scripts[0].content;
What i came to notice is that the loaded JavaScript file contents is not accessible by other JavaScript files or am i wrong ?
No, it is not possible to do that.
The source code of a script loaded via src is not exposed via any API made available to JavaScript running in the page.
You could read the value of src and then fetch it using XMLHttpRequest.
The source code of specific functions may be available by calling toString() on them.
aspx file
string firsrName="jafer";
myscript.js
GetMyName();
function GetMyName() {
alert('<%=firstName%>');
}
I am not getting my value
The line alert('<%=firstName%>'); use the Web Form Page syntax. It is actually not possible to get the value like this because this syntax cannot be used in external JS files.
The simpliest (but not cleanest) method is to write the JS method into the layout file or another aspx file.
Read How to get asp.net client id at external javascript file
You could make a global variable in your aspx page and access it in your js using window.objectName
I want to put following code in my .js file. It is developed in MVC 5 for using resx file. It is working well if I put this script in view. But is shows error when I put that code in external js file.
<script type="text/javascript">
#using myres = Microsoft.IT.GetDevices.Web.Properties;
$("#divlblNewsSource").text("#myres.Resource.NewsSources");
</script>
You cannot run razor on script files. For this you should use a cshtml file.
Declare a variable and assign the value from the resx file and inside your external JS use this variable for setting the text value.
Inside View
#using myres = Microsoft.IT.GetDevices.Web.Properties;
var temp = "#myres.Resource.NewsSources";
Inside external JS
$("#divlblNewsSource").text(temp);
EDIT:-
You can use RazorJS
It allows C# or VB.NET inside your JavaScript Files. Also contains an http handler for serving, when needed, those files.
I am trying to include external javascript to a document. Let's say that external js has the following code.
function myFunction() {
console.log("hello");
}
and I include it from console by
var script = document.createElement('script');
script.src = "http://myjs";
document.getElementsByTagName('head')[0].appendChild(script);
but then I still get myFunction() is undefined error. The function is being called from php file included on the page somehow. Interestingly, appending my external javascript right after the head tag in the document was not enough for it to precede the function call from the loaded php file.
Q: how do I ensure that I include my javascript BEFORE the php file given my situation?
EDIT: this is the hierarchy of all the sources
mysubsite.com
myfolder
mypage.html
mysite.com
myfolder
main.php
problem.php
problem.php is calling function myFunction() but it's not included anywhere yet. So I try to define the function in an outside js file and include it in mypage.html, but problem.php still comes before the included javascript in mypage.html
EDIT:
I think the real problem is that I am dealing with an iframe that's included inside a main document. In this case, is there a way to include my external javascript file inside the main document from console? Including scripts from console only affects the iframe instead of the main document.
What you're doing is fine, but then you have to wait for the file to load. Most browsers will raise the load event on the script element although some older versions of IE use onreadystatechanged instead. But since you're using jQuery (from the tags on the question), you don't have to worry about that, you can just use $.getScript:
$.getScript("http://myjs", function() {
// The script has been loaded, you can call myFunction now
});
PHP is always included before JavaScript. PHP is executed by the server before it's sent to the client, and JavaScript is executed by the client.
Why can't you just put a regular <script type="text/javascript" src="http:/myjs"></script> in your head?