473,395 Members | 1,558 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.

document.write

Hi,

I am trying to get javascript to write the following code into my page, however it will not show

Expand|Select|Wrap|Line Numbers
  1. <ul class="basictab">
  2.     <li class="selected"><a href="my menu test.htm">Home</a></li>
  3.     <li><a href="slashdot_menu.html">Slashdot Menu</a></li>
  4.     <li><a href="block_menu.htm">Block Menu</a></li>
  5.     <li><a href="shadow_test.htm">Shadow Test</a></li>
  6. </ul>
  7.  
This is the javascript that I have have tried
Expand|Select|Wrap|Line Numbers
  1. <!--
  2. function insertmenu(text){
  3. var object = text.className
  4. object.className = "selected";
  5. }
  6. function writeJS(){
  7. var str='';
  8. str+='<ul class="basictab">';
  9. str+='<li name="home" class=""><a href="my menu test.htm">Home<\/a><\/li>';
  10. str+='<li name="slashdot" class=""><a href="slashdot_menu.html">Slashdot Menu<\/a><\/li>';
  11. str+='<\/ul>
  12. document.write(str);
  13. }
  14. //-->
  15.  
The insertmenu function needs to be able to change the class of the chosen list item to selected

Could you please help

Thnx in advance
Nov 30 '07 #1
12 2029
pronerd
392 Expert 256MB
What calls these methods, and when? If you call document.write() after the page has loaded, what you are doing is over writing the entire page with that HTML.

You may want to try setting .innerHTML to add code.

Expand|Select|Wrap|Line Numbers
  1.  
  2.     var someElement = document.getElementById('someTagID');
  3.     someElement.innerHTML = '<<YOUR HTML HERE>>'
  4.  
  5.  


Hi,
The insertmenu function needs to be able to change the class of the chosen list item to selected
So are you saying this currently does not work? The code looks correct. What event is it triggering on?
Nov 30 '07 #2
it needs to write it onload along with all of the other html as it is for a horizontal menu

p.s. I have the same script without the insertmenu function on some of my other pages only without the list and that works ok as it is just working with hyperlinks
Nov 30 '07 #3
pronerd
392 Expert 256MB
You didnt answer any of my questions. There is not much we can tell you without at lest some details. How specificly is this being called. To avoid confustion it is easiest to provide the code that calls them. (Inside code tags.)

Are both functions being called at the same time? What happens when the are called? Error messages of any kind?

If it works on other pages without the insert function, have you tried removing the insert function on this page to see if it works then? Is the object that is passed to the insert function being called before the tag is on the page? i.e. Is it called inline, and above it in the code?
Nov 30 '07 #4
this is the current coding inside my page

Expand|Select|Wrap|Line Numbers
  1. <script language="Javascript" src="js/linktop.js">
  2. <!--
  3. writeJS();
  4. insertmenu('home');
  5. //-->
  6. </script>
  7. Other html
  8.  
and I have tried it without the insert function yet it still doesn't work, it just doesn't want to write out the list
Dec 2 '07 #5
acoder
16,027 Expert Mod 8TB
When you use the src attribute of the script tag, you can't add some more JavaScript within the script tags. Close them and start new ones for the inline code:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" src="js/linktop.js"></script>
  2. <script type="text/javascript">
  3. writeJS();
  4. insertmenu('home');
  5. </script>
Dec 2 '07 #6
it's working gr8 now thnx for all of ur help
Dec 5 '07 #7
last post was incorrect I had forgotten to remove something which made me think that it was working sorry, it is still not working and it comes up with an error "'undefined' is null or not an object" on line 4 of the script
Dec 5 '07 #8
Death Slaught
1,137 1GB
last post was incorrect I had forgotten to remove something which made me think that it was working sorry, it is still not working and it comes up with an error "'undefined' is null or not an object" on line 4 of the script
I've got a code that does this at my house somewhere, i'll post it later.

Thanks, Death
Dec 5 '07 #9
acoder
16,027 Expert Mod 8TB
last post was incorrect I had forgotten to remove something which made me think that it was working sorry, it is still not working and it comes up with an error "'undefined' is null or not an object" on line 4 of the script
You're passing a string to the insertmenu function, not an object. You need to get the className property of the li element, not a string. Give the li elements ids and then pass the id as the argument. Then use document.getElementById(passed_id) to get hold of the element.
Dec 6 '07 #10
Hello!

Yes, as acoder suggested, you should use the id property of the element and use document.getElementById() to select that element, instead of name tag. But this can also be done using document.getElementsByName which returns an array of elements(in our case we will have only value in the array). So, your function should look like this:
Expand|Select|Wrap|Line Numbers
  1. function insertmenu(text){
  2.     var object = document.getElementsByName(text);
  3.     object[0].className = "selected";
  4. }
  5.  
My complete test page is this:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function insertmenu_(text){
  5.     var object =     text.className;
  6.     object.className = "selected";
  7. }
  8. function insertmenu(text){
  9.     var object = document.getElementsByName(text);
  10.     object[0].className = "selected";
  11. }
  12.  
  13. function writeJS(){
  14.     var str='';
  15.     str+='<ul class="basictab">';
  16.     str+='<li name="home" class=""><a href="my menu test.htm">Home<\/a><\/li>';
  17.     str+='<li name="slashdot" class=""><a href="slashdot_menu.html">Slashdot Menu<\/a><\/li>';
  18. str+='<\/ul>';
  19. document.write(str);
  20. }
  21. writeJS();
  22. insertmenu('home');
  23. </script>
  24. </head>
  25. <body>
  26. </body>
  27. </html>
Hope this answers your question.
Dan
Dec 6 '07 #11
yes this is working great now thanks for all of your help
Dec 6 '07 #12
acoder
16,027 Expert Mod 8TB
Glad it helped. Post again if you have any more questions.
Dec 6 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: techy techno | last post by:
Hii Just wanted to know how can I decorate my texboxes and Listmenu which is called from a JS file using the following code below: document.write("<SELECT NAME='cur2' ONCHANGE='cconv1();'>");...
2
by: Brett Baisley | last post by:
Hello I have a block of html code that I want to run by calling a javascript function to print it. Its basically a table with menu items in it that is the same for many pages, and instead of...
14
by: Eli | last post by:
I've got a script that I'm trying to debug which uses document.write() to place HTML within a page. In both IE6 and Firefox when I view source, I see only the script itself and not any HTML as...
4
by: Prowler | last post by:
In the application we are currently building, we need to write positioning code on-the-fly, based upon the screen offset of the element in the AS/400 application which drives the Web app. The 400,...
2
by: bissatch | last post by:
Hi, I am trying to use JavaScript to write a table column on a web page. The code is as follows: <html> <head> <script> function displaycount() {
11
by: Tony | last post by:
Is it me, or is document.write just about the most abused js function? Maybe, like goto, js would be better without it? Is there any good reason to use it? Because I'm having a hard time seeing...
1
by: anupamaavadutha | last post by:
hi all, iam new to javascript. i have problem calling javascript functions.iam designing a calender page.here is my code. <%@ page...
8
by: Mateusz Viste | last post by:
Hi, I am trying make some multimedia files playable from my website. So far, I am able to generate dynamically a new page containing the right <embed> section. However, when I load my script, it...
1
by: celeroSolutions | last post by:
This code works in my site in IE, but not in FireFox, and I'm stuck as to why! Any ideas? (The image paths are correct, I've tested these.) <script language="javascript" type="text/javascript">...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.