Unable to load js file from php - javascript

I have created jquery functions and put them in a js file and called the file from my php using <script src="newajax.js"></script>.
This was working and now it has stopped working.
If i copy all my functions into the php file between the <script></script> tags, It works. I know there is not any issue with the js script.
To test I only added
$(document).ready(function(){
alert("ready");
});
into the js file and called it from my php. It did not show the alert but when I did this inside the php file
<script>
$(document).ready(function(){
alert("ready");
})
</script>
It worked.
So I know there isn't any issue in the php file. It just looks like the script is not being picked up and it was before with exactly the same code. I have not removed the files to a different location. The js files are in the same directory as the php file.
Anybody encountered such an issue? Or know a reason why this could happen.
FYI - I know they are html tags but I have used php extension to be able to run php functions in my file.
The jquery.js file is loaded first before my js file.
When I loaded the webpage in a new tab, it worked. I was refreshing the same browser window when the .js script stopped being called.

Related

JavaScript file doesn't effect new file imported via jquery load() method

I have a file index.php and there is a main js script in its header.
I'm using Jquery load method to load other php file inside a div element:
$("#formHideinAjax").load('loginpass.php');
And as our friend explained here the main js file which had loaded in the header doesn't work in new imported file so I was forced to add the js file inside the loginpass.php too(Now I have two same js file, one in header and one in div that loads loginpass.php ! )
I know that this method of loading js file is not standard and sends more request to my server.
How can I fix this problem?
Well, it is explicitly said in $.load() documentation. I'm afraid you can't just paste <script></script> code into your DOM for browser to run it.
I'm afraid you have to send JavaScript code from your loginpass.php file (without <script></script> tags) and just eval it in your JavaScript
$("#formHideinAjax").load('loginpass.php', function () {
// eval JavaScript code from php file here
});

Js Function not defined when I use php include

I used to write JS inline in my footer but since it's getting too much I want to write it in an external file and include the file in the footer then.
I'm currently using
<?php include "https://example.com/myjs.php"; ?>
in the footer.
In that Php file which is mainly js with a few php expressions, I'm defining all the functions first and afterwards I'm calling some of them functions "onload" with
jQuery(document).ready(function() {
// functions....
});
I've put everything inside
<script></script>
The thing is that I get an error showing the functions I'm calling arent defined. However, when I just paste the code in the footer template inside
<script> </script>
tags it works fine.
I'm assuming that the error is with the tags and inlcude...
What am I doing wrong?
EDIT:
I am using WP functions which are based on logged in user id and page id. Do these might cause the errors?
I'm using wordpress with jQuery.
Since it's a remote URL, all php scripts will be executed before inclusion into your file. So how about
echo file_get_contents("https://example.com/myjs.php");
I could solve the problem:
I defined the php vars in JS before the script within the footer.php. Credits to: Access PHP var from external javascript file
Then I just replaced the php vars wih the js vars and included it with html as script.
Works like a charm now,
thanks anyway for all the help!

How to run the .html file in browser?

I have just wrote a javascript file which I ran the via intelliJ using a few different browsers e.g. Google Chrome. This works fine as it runs locally e.g. http://localhost://.
However I want to be able to send it to someone so they just click on the .html file (packaged in correct folder) and it appears on their browser. Is there anyway I can do this? Right now the url points locally e.g. file:///Users/**/project/file.html.
Use a relative path. If your HTML file is in the same folder as the JS file, this means simply including it like so:
<script src="jsfile.js"></script>
If it's in a subdirectory, include all the folders necessary to get there:
<script src="subdirectory1/subdirectory2/jsfile.js"></script>
If it's up a directory, use the .. path:
<script src="../anotherfolder/jsfile.js"></script>
Or just include it in the HTML page itself:
<script>
// your code here
</script>

Executing external .js file from HTML page, by sending input.xml and getting output.xml from external.js file

How to execute a external Javascript file from html page.
I need to send input to .js file
And I also need to get output from external .js file.
I am thinking like building a input.xml file with my inputs as nodes in it.
I will execute the external .js file by sending my input.xml file.
My external .js file will parse my input.xml and perform the function using my inputs.
External .js will build the Output.xml file with results as a nodes in it.
My html page needs to parse the Output.xml and display the results in the html page.
I can do all the other stuff, but I couldn't figure how to execute the external .js file by sending my input.xml and read Output.xml from .js file.
Any ideas for this kind of design??
path/to/file.js:
function externalfunction(input) {
//stuff
};
index.html:
<script src="path/to/file.js"></script>
<script>
var somevar = externalfunction(someothervar);
</script>
The external script path/to/file.js is included in index.html, so the function defined in it is accessible. The function is then called.

getting the content of script file using a local javascript

similiar How can I get the content of the file specified as the 'src' of a <script> tag? and Getting content of a script file using Javascript , I want to use script tag to load xml files for the use of a javascript code - only I need it to run locally.
so how can I access the content of the files loaded using script tag?
You can not achieve this one locally.
Loading the script will execute it and won't give you access to the src
so then the only option you would have is to load the file as an ajax call. But ajax calls don't work locally (due to security) .

Categories

Resources