how to open a document when loading a web page [duplicate] - javascript

This question already has answers here:
Javascript Excel OpenFile
(2 answers)
Closed 5 years ago.
I want to open a excel document while loading a web page but i don't know how. I tried this:
<script>
function myFunction() {
window.open("file:///D://Files/doc.xlsx");
}
</script>
<body onload="myFunction()">
</body>
But it didn't work. I want to mention that it has to be done for IE8 and less.

Add tag <script> to <head>
<head>
<script>
function myFunction() {
window.open("file:///D://Files/doc.xlsx");
}
</script>
</head>
<body onload="myFunction()">
</body>

Related

Accessing variables from different files [duplicate]

This question already has answers here:
Share data between HTML pages
(4 answers)
Closed 2 years ago.
I have a project I'm working on that has multiple pages. Both of them have a JavaScript tag.
I want to be able to get a variable from the other one.
First HTML
<html>
<head></head>
<body>
<script>
var example = "stringText"
</script>
</body>
</html>
Second HTML
<html>
<head></head>
<body>
<script>
console.log(example)
</script>
</body>
</html>
I have tried making a .js file and saving a variable there and accessing it from the other file but it didn't work, the variable would clear itself when it got to the other page.
It is a possible way to approach.

Why is onclick not working in JavaScript? [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 3 years ago.
I wrote a simple code to learn Eventhandling in JavaScript but when I click on the button "Click" the function hello() does not work, why?
html file:
<!DOCTYPE html>
<html>
<head>
<script src="hello.js"></script>
</head>
<body>
<button id="click">Click</button>
</body>
</html>
JavaScript file:
document.getElementById("click").onclick = hello;
function hello() {
alert("You Clicked!");
}
You are calling the script before the dom is generated. experiment to call the right before the tag.

why i can't see the alert on my basic html (with js file ) page ? [duplicate]

This question already has answers here:
Why don't self-closing script elements work?
(12 answers)
Closed 4 years ago.
just start learn web dev ..
i created two files
- html file
- js file
the js file i wrote code:
function func1()
{
alert("alert message");
//document.writeln("document write line");
//console.log("this is log message");
}
The html file code:
<html>
<head>
<script type="text/javascript" src="basicFile.js"/>
</head>
<body>
<script>
func1();
</script>
</body>
Why the alert does not popup ?
also the doc.writeln does not work.
When i write the js func1 directly on the html - its work fine
Because you need to close script tag differently. Do
<script type="text/javascript" src="test.js"></script>
instead of
<script type="text/javascript" src="test.js" />

Put getElementById() at the bottom [duplicate]

This question already has answers here:
$(document).ready equivalent without jQuery
(39 answers)
Closed 8 years ago.
I have the following code
<html>
<head>
<script>
document.getElementById("txt").innerHTML = "Hello";
</script>
</head>
<body>
<div id="txt"></div>
</body>
</html>
and of course the text "Hello" isn't displayed because it's before the div. However because i use egl i can put js code only on head.
Is there any way to fix this?
You need to wait for your page to be ready before your code can execute on markup in the page. For example, use window.onload handler as in the example below. There are nicer ways to do this such as jQuery methods, but this will serve as an example of what you need.
<html>
<head>
<script>
window.onload = function ()
{
document.getElementById("txt").innerHTML = "Hello";
}
</script>
</head>
<body>
<div id="txt"></div>
</body>
</html>

Why can't javascript find a div that is defined below it? [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 6 years ago.
This works:
<html>
<body>
<div id="bla"></div>
<script type="text/javascript">
var mybla = document.getElementById('bla')
</script>
</body>
</html>
This doesn't:
<html>
<body>
<script type="text/javascript">
var mybla = document.getElementById('bla')
</script>
<div id="bla"></div>
</body>
</html>
mybla is null at this point. argh. How can I make this work? Thanks!!!
(and yes, I want the div below the script)
Because the DOM isn't fully loaded yet. You need to put your code in an onload handler if you want it above the HTML. Like this:
<script type="text/javascript">
window.onload = function() {
var mybla = document.getElementById('bla');
}
</script>

Categories

Resources