|
|||
|
Hi everyone,
I'm super-newbie here... so thanx in advance for any help I shoud give a "value" to an DIV's ID that change following the name of the directory where the file is. For example the file is here: www.website.com/web/01/file.html Is it possible with javascript to give the value "01" to the DIV's ID? <div id="namedirectory"> something inside </div> so it could be readed as: <div id="01"> something inside </div> I found in internet this one: <script language="JavaScript"> fullpath=location.pathname; document.write(fullpath); document.write('<br />'); result=fullpath.split("\\"); document.write(result[5]); </script> it's something that could work? I don't know how to recall the value in the ID... Thanx a lot Cheers, Luca |
|
|
||||
|
||||
|
|
|
|||
|
Luca wrote: > Hi everyone, > I'm super-newbie here... so thanx in advance for any help > > I shoud give a "value" to an DIV's ID that change following the name of the > directory where the file is. > > For example the file is here: > www.website.com/web/01/file.html > > Is it possible with javascript to give the value "01" to the DIV's ID? > <div id="namedirectory"> something inside </div> > so it could be readed as: > <div id="01"> something inside </div> > > I found in internet this one: > > <script language="JavaScript"> > fullpath=location.pathname; > document.write(fullpath); > document.write('<br />'); > result=fullpath.split("\\"); > document.write(result[5]); > </script> > > it's something that could work? > I don't know how to recall the value in the ID... > > Thanx a lot > Cheers, > Luca Hi Luca. If I understand what you want to do correctly, you should be able to do this. var div = document.getElementById('01'); div will now become a pointer to the dic element with th id of 01. I would just like to point out, that as a rule of thumb, ALL ids should start with a letter, and not a number. 'd1' would be an acceptable id name, whereas '1d' would not be as it starts with a number. Also, ids need to be unique. If you wanted to list all the div elements in the document, you can do this with a while loop like so: var divs = document.getElementsByTagName('div'); for (var i = 0; i < divs.length; i++) { // Do stuff with div her, perhaps alert it's name if it has one, like so... alert(divs[i].name); } If you want to dynamically change the id of the dive (not that you should need to), you can do this: var div = document.getElementById('01'); div.id = 'new_id'; Nice and easy. ![]() I hope this helps and I haven't completely missed the question. All the best. Daz. |
|
|||
|
Luca wrote: > Hi everyone, > I'm super-newbie here... so thanx in advance for any help > > I shoud give a "value" to an DIV's ID that change following the name of the > directory where the file is. > > For example the file is here: > www.website.com/web/01/file.html > > Is it possible with javascript to give the value "01" to the DIV's ID? > <div id="namedirectory"> something inside </div> > so it could be readed as: > <div id="01"> something inside </div> > > I found in internet this one: > > <script language="JavaScript"> > fullpath=location.pathname; > document.write(fullpath); > document.write('<br />'); > result=fullpath.split("\\"); > document.write(result[5]); > </script> > > it's something that could work? > I don't know how to recall the value in the ID... > > Thanx a lot > Cheers, > Luca Hi Luca. If I understand what you want to do correctly, you should be able to do this. var div = document.getElementById('01'); div will now become a pointer to the div element with th id of 01. I would just like to point out, that as a rule of thumb, ALL ids should start with a letter, and not a number. 'd1' would be an acceptable id name, whereas '1d' would not be as it starts with a number. Also, ids need to be unique. If you wanted to list all the div elements in the document, you can do this with a while loop like so: var divs = document.getElementsByTagName('div'); for (var i = 0; i < divs.length; i++) { // Do stuff with div her, perhaps alert it's name if it has one, like so... alert(divs[i].name); } If you want to dynamically change the id of the dive (not that you should need to), you can do this: var div = document.getElementById('01'); div.id = 'new_id'; Nice and easy. ![]() I hope this helps and I haven't completely missed the question. All the best. Daz. |
|
|||
|
Luca said the following on 12/21/2006 9:37 AM:
> Hi everyone, > I'm super-newbie here... so thanx in advance for any help > > I shoud give a "value" to an DIV's ID that change following the name of the > directory where the file is. > > For example the file is here: > www.website.com/web/01/file.html > > Is it possible with javascript to give the value "01" to the DIV's ID? Yes, but, 01 is an illegal ID. ID'es can't begin with a number. > <div id="namedirectory"> something inside </div> > so it could be readed as: > <div id="01"> something inside </div> > > I found in internet this one: > > <script language="JavaScript"> > fullpath=location.pathname; > document.write(fullpath); > document.write('<br />'); > result=fullpath.split("\\"); > document.write(result[5]); document.write(result[result.length-2]); will give you the folder name. <script type="text/javascript"> fullpath=location.pathname; result=fullpath.split("\/"); document.write('<div id="' + result[result.length-2] + '">); </script> -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ |
|
|||
|
Daz said the following on 12/21/2006 10:08 AM:
<snip> > If I understand what you want to do correctly, you should be able to do > this. > > var div = document.getElementById('01'); > > div will now become a pointer to the div element with th id of 01. He doesn't want a pointer, he wants to assign the id to it. document.getElementById('someDiv').id = newID; > I would just like to point out, that as a rule of thumb, ALL ids should > start with a letter, and not a number. That isn't a "rule of thumb" though, it is a rule of the specs that ID'es can't begin with a number. -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ |
|
|||
|
Thanx so much Daz & Randy... I've learned that 01 is an illegal ID name
![]() well, my english is not the best so excuse me. What I meant is easy to think but difficult to write ![]() I want to change the ID value in a DIV. This value should be the same value of the directory name where the file is allocated. I try again with an example: I have the file "example.html" It is in a place in the web, for example the url is: http://www.siteweb.com/directory/aboutus/example.html so... in the htlm I have a DIV that inclused other stuff... for example: <div id="JAVA"> some other code here </div> well... I need to change the value JAVA with the name of the subfolder "aboutus" that is the name of the folder where example.html is So if I will change the name of the subfolder in "contacts" the value JAVA will be automatically changed. Hope that's clear enough ![]() Sooo sorry for my english Luca "Daz" <cutenfuzzy@gmail.com> ha scritto nel messaggio news:1166713689.497160.31390@a3g2000cwd.googlegrou ps.com... > If I understand what you want to do correctly, you should be able to do > this. > > var div = document.getElementById('01'); > > div will now become a pointer to the div element with th id of 01. > > I would just like to point out, that as a rule of thumb, ALL ids should > start with a letter, and not a number. > > 'd1' would be an acceptable id name, whereas '1d' would not be as it > starts with a number. Also, ids need to be unique. > > If you wanted to list all the div elements in the document, you can do > this with a while loop like so: > > var divs = document.getElementsByTagName('div'); > for (var i = 0; i < divs.length; i++) > { > // Do stuff with div her, perhaps alert it's name if it has one, > like so... > alert(divs[i].name); > > } > > If you want to dynamically change the id of the dive (not that you > should need to), you can do this: > var div = document.getElementById('01'); > div.id = 'new_id'; > > Nice and easy. ![]() > > I hope this helps and I haven't completely missed the question. > > All the best. > > Daz. > |
|
|||
|
Luca said the following on 12/21/2006 11:07 AM:
> Thanx so much Daz & Randy... I've learned that 01 is an illegal ID name ![]() > > well, my english is not the best so excuse me. > > What I meant is easy to think but difficult to write ![]() > > I want to change the ID value in a DIV. This value should be the same value > of the directory name where the file is allocated. My first reply shows how to get the folder name. My reply to Daz shows how to set the ID attribute after it has loaded. fullpath=location.pathname; result=fullpath.split("\/"); document.getElementById('JAVA').id = result[result.length-2]; -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: Processing multiple CSV files from a directory | Peter Crawford | Newsgroup comp.soft-sys.sas | 0 | 05-09-2005 09:14 PM |
| Re: Directory structures in MS-DOS | Alan Churchill | Newsgroup comp.soft-sys.sas | 0 | 05-02-2005 03:08 PM |
| Re: How to dynamically switch the directory of the "WORK" library | Ian Whitlock | Newsgroup comp.soft-sys.sas | 0 | 04-21-2005 06:17 PM |
| Re: How to dynamically switch the directory of the "WORK" library | Terjeson, Mark | Newsgroup comp.soft-sys.sas | 0 | 04-21-2005 05:18 PM |
| Re: use a directory a condition for a loop | Workman, Rob | Newsgroup comp.soft-sys.sas | 0 | 02-22-2005 04:04 PM |