Tropilac
Tropilac

Reputation: 43

Can I document.write from a url?

I'm wanting to have a message on every page that I can change with ease. I'm very new to JS but have worked with HTML and CSS for over 3 years now.

This is what I've got so far.

<script>document.write(url="/message.html");

The purpose of this is almost like an iFrame. I want a message on every page that I can change with ease. I want to use JS because it is only one line of code I want to print.

<p>NEW!!! - GO CLICK <a href="#">HERE</a> FOR DETAILS</p>

It would be very annoying and difficult to change this single line of code on 30 or so different pages. I would like to have a single file (message.html) and have that displayed.

More info...

Thank you in advance.

Upvotes: 0

Views: 4658

Answers (4)

Tropilac
Tropilac

Reputation: 43

I discovered that I can create a single line of script code that will run a .js file.

<script type="text/javascript" src="message.js"></script>

'message.js'

document.write("<p>This is my message.</p>");

Works well and does exactly what I wanted to do. Thank you to all who helped.

Upvotes: 0

Harsha W
Harsha W

Reputation: 3366

You can use jQuery for this. Refer the below code. Add your public contents to the page.html

<script>$("#test1").load("http://example/page.html");</script>
<div id="test1"></div>

Upvotes: 0

Harsha W
Harsha W

Reputation: 3366

Is this what you want to do ?

<!DOCTYPE html>
<html>
<body>

<p>Click the button to display a string as a hyperlink.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var str = "Testing!";
    var result = str.link("http://www.google.com");
    document.getElementById("demo").innerHTML = result;
}
</script>

</body>
</html>

Upvotes: 0

Jasmit Tarang
Jasmit Tarang

Reputation: 135

// using jQuery to load a template to id result
$( "#result" ).load( "/templates/message.html" );

More info here http://api.jquery.com/load/

Not sure what you are using . I will just assume you're using jQuery at least. IF you use a framework they all have templating solutions.

Upvotes: 1

Related Questions