PDA

View Full Version : Help - .js File !



FunaGuy
12-27-2007, 12:23 AM
I added .js javascript file to my site all pages and i want to show text msg above all pages thought .js...so how can i add message in .js to show on all pages ?

Master_script_maker
12-27-2007, 12:53 AM
remove the
<script></script> code from the script and save it as something.js where something being the name. in every page you want it in add
<script type="text/javascript" src="path/something.js"></script> or if it is in the same directory add:
<script type="text/javascript" src="something.js"></script>

FunaGuy
12-28-2007, 03:37 AM
Its not working

I put this in .js & never showing the test top of the pages

<div>EXPLAME TExT</div>

codeexploiter
12-28-2007, 03:55 AM
Its not working

I put this in .js & never showing the test top of the pages

If you want to show some message in your pages you need to do the following things:

1. You need to provide some container/placeholder where the value can be displayed (divs, spans, inputs, etc).

2. You can create a new element in the document and display the message in it using the DOM based methods.

3. Each page will have a container/placeholder in which the message is preloaded but the placeholder/container is hidden using the JavaScript we show/hide the container based on your requirement.

If you directly place any HTML tags inside your JS file it is not going to bbe displayed as you've expected.

FunaGuy
12-28-2007, 04:08 AM
i really dont know how to do that

can u please make it for me...i really need it bcz i cant edit the whole pages of my website to add text (e.g update, news, note .etc) so please if u can make it in .js its really nice for me.

codeexploiter
12-28-2007, 04:18 AM
1. You can place the first section within your <script> tags:

window.onload = function(){
document.getElementById('mesg_container').style.display = 'block';
}

2. Place the below mentioned item in each of your page where you need to show the message. Within <body> tags. You can have a CSS style which is needed for your purpose.
<div id="mesg_container" style="display:none;">Your message goes here</div>

The idea is the HTML file will have a mesg_container element where you stored the message and initially it will be hidden and when the page loads the container will be shown.

I don't know why you would like to show the message using JavaScript....

FunaGuy
12-28-2007, 04:54 AM
I have unlimited pages and its very hard to add that <div> in each pages...thats y i am asking for javascript .js...bcz .js file link already added in all pages i dont need to edit each pages everything just put the msg in .js file and msg suld be show

codeexploiter
12-28-2007, 05:29 AM
Check the following code in which the JS code inserts a DIV element which contains the message in between two already existing elements:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Dynamic Element Insertion</title>
<style type="text/css">
#mesg_container {
width: 120px;
padding: 2px;
background-color: #e5eef9;
color:#000;
border:1px solid red;
}
</style>
<script type="text/javascript">
function initMsgElemInsert(){
var bdy = document.body;
var newEl = newElem('div','message goes here');
bdy.insertBefore(newEl,document.getElementById('sec'));
}

function newElem(tag,value){
var newEl = document.createElement(tag);
newEl.appendChild(document.createTextNode(value))
newEl .id = 'mesg_container';
return newEl;
}

window.onload = function(){
//setTimeout('initMsgElemInsert()',1000);
initMsgElemInsert();
}
</script>
</head>
<body>
<div id="first">First Div</div>
<div id="sec">Second Div</div>
</body>
</html>

FunaGuy
12-28-2007, 06:38 AM
bro, i think u misunderstood...i want this every thing in .js file not in html...i dont want to touch html pages...whenever i want to change the message just open .js file and change the msg text and upload on web...thats it

codeexploiter
12-28-2007, 07:12 AM
First of all where exactly you want to show your message? You didn't mention the position so it is difficult to tell you how you can place your message in your pages.

Secondly if you check my JS function it demonstrates how you can insert an HTML element without touching HTML pages if you know the structure of your pages you can modify the JS code in a manner that fits for your purpose.

My code was just to demonstrate how the elements is being inserted dynamically using JS code.

FunaGuy
12-28-2007, 06:50 PM
this is my site
http://www.funarulez.com

I want message on upper of page means top...from the starting page...

which one u mention the script...if i have to really edit html pages then i dont need that one...can u tell me how can i put iframe top of page throught .js file

FunaGuy
12-30-2007, 09:49 PM
I made up and thanks for ur time...!


This code i made last night...put this code in .js file and message will appear on each page of website where .js linked


document.write('<div style="border:1px solid #cccccc; BACKGROUND: #F4F4F4; MARGIN: 4px auto; OVERFLOW: hidden; WIDTH: 765px;

HEIGHT: 21px"><div style="font-size:15pt; text-align: center;">Happy New Year</div></div>'
);

codeexploiter
01-02-2008, 03:20 AM
I am sorry i was a bit busy with my work last day. Here is another method for doing the same which is fully DOM based one compared to the one you've developed.


/*
Insert the following items within the <style> tags
*/
#outerdiv{
border:1px solid #cccccc;
background: #F4F4F4;
margin: 4px auto;
overflow: hidden;
width: 765px;
height: 21px"
}

#innermsgdiv{
font-size:15pt;
text-align: center;
}




function putMesg(){
//New element ceations
var div1 = document.createElement('div');
var div2 = document.createElement('div');

//Assigning a unique id to the newly created elements
div1.id = "outerdiv";
div2.id = "innermsgdiv"';

//Assigning message into a variable
var msg = "Wish you a very happy and proseperous new year 2008";

//Appending the a text node which contains the message to the inner most container
div2.appendChild(document.createTextNode(msg));

//Appending the inner most container to the outer container
div1.appendChild(div2);

//Inserting the message container as the first child node in the body section of the document.
document.body.insertBefore(div1,document.body.childNodes[0]);
}


1. I would prefer to avoid the inline styles so that we can separate presentation from within the JavaScript code.

2. You need to call the putMesg function once the DOM loaded completely. window's onload event would be an ideal candidate.