How to run another program from HTML - javascript

I have the following snippet of the code:
<html>
<head>
<title>Example of Very First PHP Script ever!</title>
</head>
<body>
<script>
function comexe(){
C:\Program Files\Notepad++\notepad++.exe C:\Documents and Settings\User\Desktop\file.txt
}
</script>
music
</body>
</html>
How can I run another program from the HTML? What am I doing wrong?

Adding this as an answer, as requested in the comments
You can't - HTML is client side, and javascript is sandboxed to protect the client PC from exactly this. Apart from the title, your script has nothing to do with PHP - which can run a filesystem command, but only on the server.

Technically you could execute a program from inline vbscript. You would normally do this in a .hta file running locally.
Something like:
<html>
<head>
<title>Example of Very First PHP Script ever!</title>
</head>
<body>
<script language="VBScript">
Sub RunProgram
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "C:\Program Files\Notepad++\notepad++.exe C:\Documents and Settings\User\Desktop\file.txt"
End Sub
</script>
music
</body>
</html>

You can not do that becasue html and javascript both client side tech. and have restriction so that it can not access resource of the client machine.

If it's a operation that you have to perform many times on client machine this could be achieved.
Just install a web server (wamp or xamp or what you want on machine) and from your page call
http://localhost/nameofscript.php
this script can execute whatever you want if you set correct permissions on the user of local webserver
Perhaps, if is one-time operation that a client must execute is not so pratic to install a webserver only for an onetime operation
Bertoli Stefano

Related

Javascript not executing using XAMPP

In my html document (which is in the xampp/htdocs directory), I'm using an external .js file. The .js file is in the same directory as my html file. I'm simply trying to use document.write() function and it's not printing anything.
I don't understand what I'm doing wrong. Whats the issue?
HTML file
<!DOCTYPE html>
<html>
<head>
<?php include 'include/head_elements.html'?>
<script type="text/javascript" src="register.js"></script>
</head>
<body>
<h1>Company Account creation</h1>
<div id="registration_menu">
<!--Elements are added and removed dynamically using JS-->
</div>
<script>
hello();
load_element_group("email_verification");
</script>
</body>
</html>
JS file
function hello(){
document.write("Hello world")
}
Internet Explorer's security policy may block certain scripts from running on a local machine.
There are ways to avoid this -- such as by adding the XAMPP website as a trusted location -- but often this gets tricky since the default "Intranet Zone" is auto-configured on a PC and modifying that can have other consequences (different zones assume different settings, such as passing NTLM credentials to local websites).
See also https://stackoverflow.com/a/7038775/3196753
A quick fix often is to add the fully qualified domain name (FQDN) to the URL, but depending on the zone settings, this may still cause issues.
A final solution, and one many developers fall back on, is to actually use a registered DNS address, such as http://localtest.me/, which points back to localhost and should use the "Internet Zone".
As Chris G points out in the comments, this isn't typical. Normally localhost can be used without issue so I've provided an example Local Intranet setting which can cause this:

The main difference between internal js file and external js file

Is there any different between include external js file and write down javascript in the html page.
Case 1
test.html
<html>
<head>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
</body>
</html>
test.js
alert('aaa');
Case 2
test.html
<html>
<head>
<script>
alert('aaa');
</script>
</head>
<body>
</body>
</html>
Case 1 execute more faster than Case2 if my memory services me right. But I am not sure. Moreover, I cannot find the relative documents or articles to support my ideal. May someone help me?
Case 1 is is slightly slower at first, because it needs to do a second request to get the script, so there is a little overhead.
However, the browser will cache the javascript file, so if you have multiple pages that share the same script, Case 2 will be more efficient, because for subsequent pages, the browser already has the cached script and doesn't need to download it again.
Also, most browsers will allow opening two connections to the same server, so it may download the page and the script simultaneously on the first request, although it depends on size of the page and the script, the server, the client and the internet properties (latency and speed) which solution is faster.
Case 1: will retreive the script from the server and run it syncrhonously (script file will be cached on subsequent requests, file can be minified)
Case 2: will have the script loaded as part of the page (no extra page request), and run when script tag is loaded into dom.
both: will hold up page at that point
Info: http://webdesign.about.com/od/speed/a/script-placement-for-speed.htm

Trying to fetch json string from internet api

I am trying to fetch the json string from this url:
http://status.mojang.com/check
however every time i get a failure or this:
XMLHttpRequest cannot load http://status.mojang.com/check. Origin null is not allowed by Access-Control-Allow-Origin.
Any help appreciated
<!DOCTYPE html> <html> <head> <title>mc stats</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script> <meta http-equiv="Access-Control-Allow-Origin" content="*"> </head> <body>
<div id="minecraft"> </div>
<script> (function() { var flickerAPI = "http://status.mojang.com/check?jsoncallback=?"; $.getJSON(flickerAPI)
.done(function( data ) {
console.log(data); }); })(); </script> </body> </html>
You need to run the webpage in a server and this problem should go away. There might be a way to do it with Google Chrome, but I am not aware of it. When I have this issue, I typically run my file with Aptana Studio. It is a free download and it causes the file to be served up from a server instead of just being read from your file system. It defaults to Firefox, but you can just paste the url in Chrome and everything should work like you expect, unless there are other errors.
If you are running a mac/linux you also have access to the python simple server and it should serve it up also. Run the command python -m SimpleHTTPServer from the file directory and you should be able to see it at localhost:8000. Good Luck!

Resource requests from webpages fail, but direct request work

I've installed CentOS and LAMP on an old desktop at home to practice system administration and webadmining, but I'm running into a strange problem.
I have a webpage that's no more than:
<!DOCTYPE html>
<html
<head>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
</body>
</html>
The chrome console is telling me that the request for test.js is failing. When I right click the link it gives and open it in a new tab, it correctly shows.
Is there a setting in apache that I need to set for this to work?
edit: Only thing in the logs is about the favicon
edit:
Some additional details: I'm accessing via the local ip of the machine, external script and stylesheet requests work, and all the files are in the same directory.
edit: Using fiddler, it looks like chrome isn't even sending out the request for the files.
edit: The response headers for the webpage include "Connection: close"
Make sure that file with that HTML code, and test.js are in the same directory.
I suspect that's the problem.
If you double click file on your system it's not opened via apache.
If you are opening html by
http://localhost/test.html
Then chrome will search to test.js in
http://localhost/test.js

Script run on server or client?

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).

Categories

Resources