This question already has answers here:
JavaScript: Inline Script with SRC Attribute?
(3 answers)
Closed 8 years ago.
When I launch this in chrome, nothing appears on the page.
<!DOCTYPE html>
<head><title>test 4000</title><head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
document.writeln("test")
</script>
</body>
</html>
you are inserting code in a script tag that you are also using to load an external script (jQuery). you should either do the one or the other.
<script>
document.writeln("test");
</script>
if you want both do:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
document.writeln("test");
</script>
the reference states:
If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI.
you do not need to include jQuery to use document.writeln()
Related
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.
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" />
This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 9 years ago.
I'm trying to get the element with getElementById(), but it returns null even though the element exists. What am I doing wrong?
<html>
<head>
<title>blah</title>
<script type="text/javascript">
alert(document.getElementById("abc"));
</script>
</head>
<body>
<div id="abc">
</div>
</body>
You have to put this in a document load event. The DOM hasn't gotten to abc by the time the script is executed.
Your script runs before the DOM has been loaded. To fix this you can place your code in the window.onload function like so:
window.onload = function() {
alert(document.getElementById("abc"));
};
An alternative is to place your script right before the closing </body> tag.
If you don't want to attach to the load event then simply put your script at the bottom of the body, so it will execute at the end-
<html>
<head>
<title>blah</title>
</head>
<body>
<div id="abc">
</div>
<script type="text/javascript">
alert(document.getElementById("abc"));
</script>
</body>
</html>
This is because the script runs before the page has rendered.
For proof add this attribute to the body tag:
<body onload="alert(document.getElementById('abc'));" >
But it doesn't exist, not at that point in the HTML. HTML documents are parsed top-to-bottom, just like programs run. The best solution is just to put your script tag at the bottom of the page. You could also attach your JavaScript to the onload event.
This question already has answers here:
style and script tags in HTML body... why not?
(7 answers)
Closed 8 years ago.
Is it possible to put things normally found in between the body tags in a script?
Like even an image:
<img src="url" alt="some_text">
Could I have that in <script> <img src="url" alt="some_text"> </script>
I want the user to be able to just copy something like :
<script type="text/javascript" src="script.js"></script>
And it would include html code too, or even something like:
<script type="text/javascript" src="script.js"></script>
<script type="text/html" src="htmlstuff"></script>
If you need to mix scripts and HTML (which I highly don't recommend) you can mix individual script blocks with that HTML. E.g.
<script>
//some JS code goes here
</script>
<img src="url" alt="some_text">
some more HTML goes here
<script type="text/javascript" src="script.js"></script>
some more HTML goes here
<script>
//some JS code goes here
</script>
But again - this is bad practice.
If you want your user grab link just to JS and have it produce HTML - your JS code have to produce that HTML programmaticaly - look up document.createElement, element.appendChild etc.
Note that if you can use jQuery - your life becomes a lot easier in both manually creating DOM elements as well as ability to load complete HTML e.g. using .load() method.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Creating a page that updates without reloading
I want to get the content of an HTML page from it's URL and put it somehwere in the current document. The pseudo-code below shows the approach I want to take:
<!DOCTYPE html>
<html>
<body>
<p id="text"></p>
<script type="text/javascript">
function getPageText(htmlPage)
{
//comment: How do I get innerHTML?
var text = htmlPage.body.innerHTML;
//comment: How do I have to return?
return text;
}
document.getElementById("text").innerHTML=getPageText("https://google.com");
</script>
</body>
</html>
The name of the technique you are looking for is AJAX --- You need to download the remote page (and it has to allow you to) in order for javascript to get the content of the page.
see another answer for an overview of that technique --- https://stackoverflow.com/a/10168402/473914
you need to extract content from google and then you could use innerHtml. Alternatively you can use iframe and wrap scr dynamically or statically. code for wraping scr dynamically is as follow.
a.html
<html>
<head>
<script>
function getPageText(url){
document.getElementById("container").setAttribute("src",url);
}
getPageText("https://www.google.com");
</script>
</head>
<body>
<iframe id='container'></iframe>
</body>
</html>