This may seem like a silly question, but consider the following use of the 'script' element to import an external javascript file:
<script src="http://localhost:8085/myscript.js" type="text/javascript" >
Given that this is a url and not a file path, would this be evaluated server side or client side?
If evaluated client side, then it would fail as the script resides on the server, not the 'localhost' of the client?
Thanks.
Client side, and yes - respectively.
The myscript.js file is downloaded from the server (localhost:8085) then executed by the browser on the client-side.
Javascript is always executed on the client-side.
Related
Maybe the problem is that i wrote path to my file incorrectly, i'm using Linux, do i have to write path in some different way?
<script type="text/javascript">
function func(){
var xmlFile = new XMLHttpRequest();
xmlFile.open("GET", "/home/kat/course/data.xml", false);
xmlFile.send();
xmlDoc = xmlFile.responseXML;
document.getElementById("result_field").value = xmlDoc;
}
</script>
The file needs to be in the webroot.
So if you have the code in /var/www and your site is 'somedomain.com/index.html' you will need to have the xml file in the same /var/www directory and access it like "GET", "somedomain.com/anotherfolder/data.xml"...
You can't access server directories from JS... The javascript runs on the client side, not on the server side, so the file needs to be accessible on the website.
JavaScript can only access files your server is serving. Unless you are serving /home/kat/course/data.xml, you're going to need to move data.xml before your JavaScript can access it. Once you move it to a location being served, it will be accessible via an XHR.
As a side note, the responseXML property of an XHR is a Document object, not a string. See: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseXML
There could be two problems. The url must be {your_server_domain}/home/kat/course/data.xml, and the document.getElementById("result_field") must be an input/textarea to accept value attribution, otherwise, use innerHTML.
P.s.: Your local server domain usually is: localhost or 127.0.0.1:80.
AM trying to implement a push server for my pHP application.
My initial thoughts were as long as i keep the EVENTS called on my client let say message_view.php file . I would not have a problem of emiting node js events.
But i see that most of the tutorial online use something like I dont understood this ?
fs.readFile(__dirname + '/client.html') ...//html files
or
response.sendfile('hello world')
After they have started the server. Then they add event and logic as follow on the client html file that am supposed to do it on my messages_view.php file.
<script src="socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
and socket emiting events like this one :
<script>
// create a new websocket
var socket = io.connect('http://localhost:8000/?profile_id=abcde2');
//..I want to code here to update my divs.
</script>
What do i need exactly to emit this message on my PHP file. Many Thanks!
If you want to serve html with php, and have a node.js socket server, the socket server doesn't need to serve html, so you can omit the majority of the example you're using. Just include the script in your .php that serves the html page and make sure the url to the script properly targets the socket server.
I want to trigger execution of a server-side script (.sh file) from JavaScript code running in a web browser.
I've searched google and no code that I've found is working for me.
You need to first make a file in a server-side language (like PHP, Python, RoR, PERL, ASP.NET, or JSP) that runs the .sh file. Then you have to use Ajax to request that page.
In PHP, something like:
<?= shell_exec('sh /home/user/public_html/scripts/script.sh'); ?>
Then, for JS, something like:
<script src="//code.jquery.com/jquery-1.8.0.min.js"></script>
<script>
var runShellScript = function () {
$.get('/scripts/script.php', function () {
alert('Shell script done!');
});
};
// Some stuff
runShellScript();
</script>
If you don't want to use jQuery or Ajax, you could probably get away with JSONP.
I believe You can't do this directly with client side JavaScript. However, you can invoke an url request to let server side language (PHP, Java, Ruby... almost any language) to do the task. And be sure to consider security issues.
Make sure you .sh file begins with:
#!/bin/sh
echo "Content-type: text/html"
echo ""
and the .htaccess file is set up to execute .sh files:
Options +ExecCGI
AddHandler cgi-script sh
Then you can simply call the file from the url using XMLHttpRequest. http://en.wikipedia.org/wiki/XMLHttpRequest, and maybe even have your script return some json object.
I don't know if Javascript opens .sh files in particular, but you can use the filereader API to open files in general.
I have a thttpd server set-up which has the following html file. When I give address server-address/file-name.html on a standard web browser errors on the script is logged in error console of browser. I am confused about where the script is run actually? Is it on the client side or are the error messages just passed on to browser by server?
My requirement is to run script on a server to generate dynamic web pages upon client interaction.
<html>
<head>
<title>Entitled Document</title>
<script language="JavaScript" >
Function Java_Scriptfn()
{
alert('Test'
}
</script>
</head>
<body>
<input type="button" value="Script_Check" onclick="Java_Scriptfn()">
</body>
</html>
That is purely client side code, so it runs on the client.
As far as I can tell, thttpd only supports server side programming via CGI.
JavaScript that is embedded in a HTML site (either inline or load from another file) is always executed client-side (that means in your browser).
If you want it to be executed, server-side, you need something like node.js.
It's client side code; any Javascript files included in an HTML page will run client-side (although they can talk to a server, that's different).
For example, having:
<script type="text/javascript"
src="http://somedomain.com/js/somejs.js?14">
</script>
So what does "?14" means here?
Its a url param like any other parameter passed in a url. Sometimes JS scripts are created on the fly using server side technologies other times it is simply a version number to help with browser caching issues.
They are there to fool browsers into thinking that it is a new file.
This is a trick to avoid browser-cached copy when you update the JS file.
It means a variable is being passed to the script via GET, though standard JavaScript files don't support any means of collecting the variable.
You could, however, write a server script in PHP or ASP.NET that sets the content type as application/x-javascript.
Like this in php:
// file: external.php
<?php header("content-type: application/x-javascript"); ?>
// regular javascript here that uses $_GET['variable'];
Then you could put this in your HTML script tag:
<script type="text/javascript" src="external.php?variable=14"></script>
The javascript script is probably generated by a server side script (PHP, CGI, etc.) , which takes 14 as a parameter.
This is a query parameter as the browser will make an http get request to the somedomain.com for the javascript source.
If you load the page with a header browser like fiddler, you will see exactly what's going on.
IMHO, a JavaScript source like this will request "dynamic" content from server, thus the server will not try to use cached version of JavaScript file. Whether or not the parameter really does matter is up to the server.