views:

99

answers:

9

Hi folks,

I'm trying to store a reference of an HTML tag for later reuse.

e.g. If I click on a div and save a pointer to that div within Javascript, will there be a way I could serialize such pointer? So I could de-serialize it and use the pointer in another instance of the web application?


Only methods I can think of are the following:

  • Use id or name attributes

  • Create a CSS selector for that element

Any other ideas guys? =)

A: 

Only logical way is to use id.

It is ussually not hard to assign id to all important elements based on database values.

Sergey Osypchuk
@sergey: problem is that not every click-able element is guaranteed an id. So I reckon that building a css selector based on common attributes would be more efficient. e.g. class href src tag ...
RadiantHex
I would limit user to select only from elements which has logical item under it.I am starting to understand your problem - you are building tool which must work with general html.
Sergey Osypchuk
@RadiantHex You can't retrieve CSS selectors in javascript without using some hackish algorithm or heavy JS library - neither efficient. XPath on the other hand is supported natively in every browser and is extremely fast.
lucideer
A: 

I've tried something like this:

{tag:"div", style:"float:left;", "class":"fancy", inner:[
  {tag:"a", href:"http://google.com", inner:"A link to google!" },
  {tag:"a", href:"http://yahoo.com", inner:"A link to yahoo!" }
]}

Seems to work okay, although it's easy to get lost with all the curly brackets.

edit - maybe I completely misunderstood what you want... if you want to serialize a handle to that element, like what would be returned by getElementById, you might as well just use the id.

no
*inner* should be an array.
Gumbo
sure should. It's missing commas too. I'll fix it...
no
A: 

It looks like JSON.stringify( yourDivReference ) and JSON.parse( serializedObjectString ) might do what you're looking for.

UPDATE: Actually, the JSON methods don't like the circular references in the DOM. See this question for more details: http://stackoverflow.com/questions/2303713/how-to-serialize-dom-node-to-json

I do, however, agree with Sergey that using the ID seems like a better way to go.

Sean McMains
+1  A: 

What exactly are you trying to save? And where exactly are you re-using it?

A DOM element would be very specific to that particular browser rendering on that page -- Just hitting refresh will give you an whole new DOM element. So, what about it do you need to save & recreate?

James Curran
+2  A: 

How about the innerHTML of the element?

R. Hill
It could be an empty element, or a repeated one. This would also be quite inefficient as it would involve searching the entire document for strings each time.
lucideer
Well, I don't know *exactly* what he wants to do, but this does match *serializing* an HTML element, what he asked -- although of course it's out of question to "reuse" the "pointer" itself. From the answers I see here, it seems many interpreted his question as "serializing the reference to an element", instead of "serializing the element itself."
R. Hill
He's requested the ability to "de-serialise and use as a pointer", so its evidently a reference. He's also given examples of using an ID, or a "CSS selector" - both references.
lucideer
Thanks guys =) I guess innerHTML would be an accurate way of trying to serialize a pointer. I'm starting to think though that I could store the tree structure of the page using tag denominations, and then just store some kind of selector for each element.
RadiantHex
A: 

Based on the way you worded your question, I don't think that would be possible. What exactly do you mean by "another instance of the web application"? Since JavaScript will be running on the client side, you won't be able to share data between clients. However, you might want to do something like store/read from a database. Can you describe more of the functionality you are trying to achieve?

elevine
+2  A: 
lucideer
+1, probably closest to what the OP was getting at
no
@lucideer: Thanks!! this is actually a version of what I was thinking. This is an excellent reply and I thank you very much!! I think I might tick this one =)
RadiantHex
yes, this is great! Thanks!
RadiantHex
A: 

XPath seems to be the most appropriate; however, only if the page structure is (relatively) static up to that node. Here are some references and code:

Method for getting the xpath of an arbitrary node and example use

getPathTo($("a")[4]):

yields

"id("hlinks-custom")/A[2]"

MDC XPath documentation

Justin Johnson
A: 

Another idea - to generate own custom id's for all elements based on some rules:

  1. element id starts from parent id. -> customid = "body/item"
  2. If normal id is available use it. if not, use element type. Than add order in current subtree.

so, you will get something like "body-0/item-0" for example above or "body-0/div-4" if id is not known.

When you will try to use your "custom id" and page will be changed, you will have a chance to find closest element, comparing all elements custom id to stored "custom id".

Sergey Osypchuk