473,396 Members | 2,023 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,396 software developers and data experts.

Reading element attribute

drhowarddrfine
7,435 Expert 4TB
I'm going to have to fire my javascript guy so I guess I better start relearning all this. ;)

I wrote a little script to change the src of an image.

<script type="text/javascript">
function mine(x){
y=document.getElementById(x);
y.src="2.png";
}

It works but I noticed that if I want to test the initial value, I have to use 'getAttribute("src"). This does not work:

if(y.src=="2.png") y.src="1.png";

I don't get any errors in the Firefox error console.
if(y.getAttribute("src")=="2.png") y.src="1.png";

I don't understand why I can't just say:

if(y.src=="2.png") y.src="1.png";

Or does it just not work that way? iow, I can set it without using getAttribute but I can't test it.
Apr 13 '08 #1
7 1429
acoder
16,027 Expert Mod 8TB
getAttribute() will return the full path in some browsers, so use lastIndexOf and substring to get the file name only, e.g.
Expand|Select|Wrap|Line Numbers
  1. y.src.substring(y.src.lastIndexOf("/")+1)
Apr 13 '08 #2
drhowarddrfine
7,435 Expert 4TB
Aha moment. What I'm doing would work but the path name is file:///usr/home.... while I was just looking for the filename alone. Your method does what I really want to do and avoids that full pathname issue. Thanks.

What did you mean about the getAttribute method by "some browsers will return the full pathname" ?
Apr 13 '08 #3
hey ..

an advice :

use event.srcElement.id;

for example

if(event.src.Element.id == 1)
document.getElementById(1).src ="image.jpg";
Apr 13 '08 #4
drhowarddrfine
7,435 Expert 4TB
I'll look at that. Thank you.
Apr 13 '08 #5
mrhoo
428 256MB
IE and Opera will return the absolute url for a src or href attribute of an element, even if it is coded relatively in the html or applied with a script. Firefox doesn't- it returns the literal relative text. I don't know what safari does.

Just knowing that there are differences, makes me always convert any path string I need to compare to another to an absolute path, so I don't have to remember which does what.

Added to that, if you want your code to work on a filesystem, either on a cd or a folder on your hard drive, you have to do flips to get the different syntaxes the browsers use for local urls. And if you have a subdomain it is handy to set a temporary site root folder for all those relative urls.

This may be overkill, but maybe you can use some piece of it:

Expand|Select|Wrap|Line Numbers
  1. document.getRoot= function(rootStr){
  2.     var L= location.href;
  3.     L= L.replace(/\\/g,'/');
  4.     var ax= rootStr? L.indexOf(rootStr): -1;
  5.     ax= (ax== -1)? L.indexOf(location.pathname)+ 1: ax+ rootStr.length;
  6.     return L.substring(0,ax);
  7. }
  8. document.getPath=function(str){
  9.     var str= str.replace(/\\/g,'/');
  10.     if(str.search(location.protocol)==0 || /^(mailto|http)\:/i.test(str)){
  11.         return str;
  12.     }
  13.     var R=document.getRoot();
  14.     if(/^\//.test(str)) return R + str.substring(1);
  15.  
  16.     var Loc= location.href.replace(/\\/g,'/');
  17.     var ax= Loc.lastIndexOf('/');
  18.     Loc= Loc.substring(0,ax);
  19.     var L= R.length;
  20.  
  21.     while (str.search('../')==0){
  22.         if(ax<L) return '';
  23.         ax= Loc.lastIndexOf('/');
  24.         Loc= Loc.substring(0, ax);
  25.         str= str.substring(3);
  26.     }
  27.     return Loc + '/' + str;
  28. }
Apr 13 '08 #6
acoder
16,027 Expert Mod 8TB
hey ..

an advice :

use event.srcElement.id;
A piece of advice, don't use event.srcElement!

It's an IE-only non-standard property to get the target of the event. Though not needed here, here's a cross-browser way to do get it.
Apr 14 '08 #7
acoder
16,027 Expert Mod 8TB
IE and Opera will return the absolute url for a src or href attribute of an element, even if it is coded relatively in the html or applied with a script. Firefox doesn't- it returns the literal relative text. I don't know what safari does.

Just knowing that there are differences, makes me always convert any path string I need to compare to another to an absolute path, so I don't have to remember which does what.

Added to that, if you want your code to work on a filesystem, either on a cd or a folder on your hard drive, you have to do flips to get the different syntaxes the browsers use for local urls. And if you have a subdomain it is handy to set a temporary site root folder for all those relative urls.
Thanks, mrhoo. A lot more detail than I would've given.
Apr 14 '08 #8

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

Similar topics

0
by: Dave Mc | last post by:
First off I apologize for the length of this post. Given the following for XML <?xml version="1.0" encoding="Windows-1252"?> <Product Name="Product1" xmlns:d2p1="InspectionFile">...
4
by: Gordon Dickens | last post by:
I have target xml to generate from schema. All of the XML instances have the same global element i.e. <base>. I would like to combine all of the schemas into a single schema where I could...
12
by: Anna | last post by:
Hi all, I posted the same question this afternoon but my message isn't showing up, so I thought I'd give it another try.... in case you should see it later I apologize for posting the same...
3
by: Fabian | last post by:
How can I read the object.style.display attribute? With teh following: var x = object.style.display; I always get a result of undefined. Solutions? -- -- Fabian
0
by: chris.millar | last post by:
I am using the ReadXMLSchema method of system.data.dataset to read a schema into a dataset. I have an XML file that I build up and wish to do a .Update against a database. The dataset doesn't...
6
by: Hans Kamp | last post by:
My program fails reading XML attributes. A fragment of the code is: private void showXmlNodeAtTreeNode(XmlNodeList xnl, TreeNode tn) { int i; for (i = 0; i < xnl.Count; i++) { XmlNode xn =...
1
by: SteveB | last post by:
I'm porting an application from Apache Xerces to .Net and am having a couple of small problems with deserialization. The XML that I'm reading comes from a variety of sources, and there are two...
0
by: djgerbavore | last post by:
I'm trying to read in a XSD file and not all my elements are showing up in my XMLSchema object. For example the following is an example xsd file i created in XMLSpy <?xml version="1.0"...
2
by: mlb5000 | last post by:
I seem to be having issues validating an XML document using my schema. Both are below: The Schema: <?xml version="1.0" encoding="UTF-8"?> <xs:schema...
16
by: billsahiker | last post by:
I am researching for an upcoming c# .net 2.0 project that may require reading and writing xml files. I don't want to use xmltextreader/ xmltextwriter as I prefer to have lower level file access...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.