473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Adding Script Element to the DOM

Currently I am trying to use JavaScript within greasemonkey to dynamically
put a menu at the top of each page. I am running in to trouble when I try
to append a node representing a script tag to the header section. It ends
up not adding anything to the document. I am currently getting no errors
from the javascript panel in firefox. Here is the code I am using:

HeadTag = document.getElementsByTagName("head");

var scriptElement = document.createElement('script');
if (scriptElement) {
scriptElement.type = 'text/javascript';
scriptElement.id = 'addedscript';
HeadTag[0].appendChild(scriptElement);

}

Please let me know if I am doing something wrong or if i need to add more to
this code. Also, how do I then add the javascript code to the newly created
node? Thanks for the help.

Joseph Scoccimaro
gt*****@mail.gatech.edu
Nov 23 '05 #1
7 3290
yb
what does the script itself do? maybe try manually adding the script
first to see its effect?

or, instead have some container element in the body of the document
where the menu can be added?

Nov 23 '05 #2
The script would just add this menu bar to the top of every page:
http://www.brainjar.com/dhtml/menubar/demo3.html. I am currently working on
a project that will use the DOM to perform different types of analysis on a
page. I just wanted this menu to be above every page so that the user can
select a type of analysis to perform.

What do you mean have a container element?
Joseph Scoccimaro
Nov 23 '05 #3
Joseph Scoccimaro wrote:
Currently I am trying to use JavaScript within greasemonkey to dynamically
put a menu at the top of each page. I am running in to trouble when I try
to append a node representing a script tag to the header section. It ends
up not adding anything to the document.
How are you determining that? Your code works for me in Firefox, I can
see the added script element in the DOM inspector.

I am currently getting no errors
from the javascript panel in firefox. Here is the code I am using:
There are tips on writing scripts here, I'll just comment on what you
posted:

<URL:http://greasemonkey.mozdev.org/authoring.html>


HeadTag = document.getElementsByTagName("head");

var scriptElement = document.createElement('script');
if (scriptElement) {
Better feature detection is:

if (! document.createElement ||
!document.getElementsByTagName) return;
...

Since you are running this from Greasemonkey and that also means the
Firefox browser, it might be reasonable to expect a certain level of
support for JavaScript and DOM and not bother.

scriptElement.type = 'text/javascript';
scriptElement.id = 'addedscript';
HeadTag[0].appendChild(scriptElement);

}

Please let me know if I am doing something wrong or if i need to add more to
this code. Also, how do I then add the javascript code to the newly created
node? Thanks for the help.


As far as I can tell, Greasemonkey inserts the code for you, you just
need to write the functions.

Below is a function that will add a script element in a generic fashion
where the code is in a separate file 'scripts/aScript.js':
function addScript()
{
if (! document.createElement ||
!document.getElementsByTagName) return;

var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.id = 'addedscript';
oScript.src = 'scripts/aScript.js';
document.getElementsByTagName('head')[0].appendChild(oScript);
}
To conform to the suggested syntax for Greasemonkey and add it to the
global object as an anonymous function, wrap it thusly:

(
function addScript()
{
...
}
)();

Why not post a very simple example of your menu (say one item) along
with how you are attaching it?

--
Rob
Nov 23 '05 #4
RobG wrote:
Joseph Scoccimaro wrote: [...]
I am currently getting no errors from the javascript panel in
firefox. Here is the code I am using:

There are tips on writing scripts here, I'll just comment on what you
posted:

<URL:http://greasemonkey.mozdev.org/authoring.html>


Downloaded and installed Greasemonkey, the following works:

(
function addScript()
{
var theHead = document.getElementsByTagName('head')[0];
var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.src = 'file://C:/fullPathToFile/blah.js';
theHead.appendChild(oScript);
}
)();

[...]

--
Rob
Nov 23 '05 #5
RobG said the following on 11/18/2005 2:04 AM:
RobG wrote:
Joseph Scoccimaro wrote:


[...]
I am currently getting no errors from the javascript panel in
firefox. Here is the code I am using:


There are tips on writing scripts here, I'll just comment on what you
posted:

<URL:http://greasemonkey.mozdev.org/authoring.html>


Downloaded and installed Greasemonkey, the following works:

(
function addScript()
{
var theHead = document.getElementsByTagName('head')[0];
var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.src = 'file://C:/fullPathToFile/blah.js';
theHead.appendChild(oScript);
}
)();


function addScript(scriptSource){
......
oScript.src = scriptSource;
}

Allows it to be a general function to add a script file on the fly.
Which way one does it depends on the needs/desires at the time.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 23 '05 #6
Thanks a lot for the help.
Joseph Scoccimaro
Nov 23 '05 #7
Randy Webb wrote:
RobG said the following on 11/18/2005 2:04 AM:

[...]
(
function addScript()
{
var theHead = document.getElementsByTagName('head')[0];
var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.src = 'file://C:/fullPathToFile/blah.js';
theHead.appendChild(oScript);
}
)();

function addScript(scriptSource){
.....
oScript.src = scriptSource;
}

Allows it to be a general function to add a script file on the fly.
Which way one does it depends on the needs/desires at the time.


Probably getting OT, but I could not get access to functions in the
script that is loaded from the function that loaded it, e.g.:

(
function (){
var addScript = function(){
var oS = document.createElement('script');
oS.type = 'text/javascript';
oS.src = 'file://fullPathToFile/xx.js';

var theHead = document.getElementsByTagName('head')[0];
theHead.appendChild(oScript);
}

var someFn = function(){
// try to use some function in xx.js
}

addScript();
someFn();

}
)();
If xx.js is:

alert('Hi from xx.js');
the alert 'Hi ...' appears. But wrap it in a function and try to call
it and the console reports that the function is not defined. Use
setTimeout() to call the function (even with a lag of 0ms) and we're
back in business:

setTimeout("someFn()", 0);
I have noticed similar behaviour at other times too. I'm sure I
stumbled across a Firefox bug report about it but can't find it again.

--
Rob
Nov 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Amir Hardon | last post by:
I'm new to DOM and can't figure out this thing: I'm trying to add a row to a table with a form field in one of it's cells, but if I'm appending the field to a form it gets out of the table. Can...
15
by: crjunk | last post by:
I have 4 TextBoxes on my form that I'm trying to add together to get a grand total. Here is the code I'm using: <SCRIPT LANGUAGE="JavaScript"> <!-- Beginning of JavaScript - function...
10
by: Melissa Mussitsch | last post by:
I've done this before while creating a brand new table. But the code below is not working and I keep getting the error: "Internet Explorer cannot open the Internet site ..... Operation aborted" ...
2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
5
by: yawnmoth | last post by:
I'm having some difficulty with adding elements to a webpage via the DOM. The following works: main.htm: <script> js = document.createElement('script'); js.src='test.js';...
2
by: Muzzy | last post by:
Hi, I've used information on these newsgroups to build many pages. So I thought that now that I have my script working (something that I've been working on for about a week), I should post it so...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
1
by: sarwarmohiuddin | last post by:
Hello MAtes, I am having this problem of adding a script element in a rich text editor box. i am doing this. The problem is that it won't add the script tag alltogather, or give an error "Unknown...
10
by: AC | last post by:
I had a page that does some event setup on window.onload: function prepEvents() { document.getElementById("menumap_sales").onmouseover = swapMenuSales; // etc } window.onload = prepEvents;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.