views:

611

answers:

2

i want to put rel=lightbox to some links that mediabox support using javascript.

i try this and wonder why it's not working?

test: http://jsbin.com/opica

please help edit this: http://jsbin.com/opica/edit

<script  type="text/javascript">
var x=xmlDoc.getElementsByTagName("a");
var regexku=/^.+(((twit)|(tweet)|(com/video.+)|(flickr.com.+)|(tube.com.+))|((gif)|(jpe?g)|(png)|(flv)|(swf)|(mp3)|(mp4))$)/;

for(i=0;i<x.length;i++)
 {
a=x[i].getAttribute('href');
if (a.match(regexku) != null)
   {
   x.item(i).setAttribute("rel","lightbox");
   }
 }

</script>
A: 

Try escaping the / between com and video.

Bart Kiers
ok. but that does not make it work
DennyHalim.com
@DennyHalim, okay, but when you escape the `/`, the regex becomes a valid one. If the regex `doesn't work`, you might try to explain what strings you're trying to match. because `doesn't work` does not tell me much.
Bart Kiers
I see on the link you posted (could you provide that information in your original post as well please?) that you're running the following test cases: `'.jpg'`, `'.gif'` , `'.png'`, `'.flv',` `'.swf'` and `'youtube.com/watch'`, all of which pass the regex you posted.
Bart Kiers
i dont know much about regex. but i tested the regex here http://www.regextester.com/ and no error.
DennyHalim.com
@DennyHalim, yes, like I said: your regex matches all the strings from your tests ('.jpg', '.gif' , '.png', '.flv', '.swf' and 'youtube.com/watch'). So the regex is correct.
Bart Kiers
+1  A: 

So if you open the Error Console (Tools -> Error Console in Firefox), you'll see two errors on your page:

Error: xmlDoc is not defined
Source File: http://jsbin.com/opica
Line: 35

Error: invalid regular expression flag v
Source File: http://jsbin.com/opica
Line: 21, Column: 38
Source Code:
var regexku=/^.+(((twit)|(tweet)|(com/video.+)|(flickr.com.+)|(tube.com.+))|((gif)|(jpe?g)|(png)|(flv)|(swf)|(mp3)|(mp4))$)/;

The later is fixed by escaping the slash as Bart suggested (com\/video).

The former says there's no such thing as xmlDoc. You probably meant the page's document, in which case you should replace it with document.

Next the whole thing probably won't work because you should run the script after the page is finished loading. In jQuery it's $(document).ready(function() { /* do your work here */ }), google how to do it using the whatever framework you're using (mootools-yui?).

After that as you can see, the rel attribute is set on the links: http://jsbin.com/elaca/edit. The fact that the whatever library you're using still doesn't work means you're using it wrong. You didn't even link to the page you've downloaded the library from so that someone could look up the documentation for you...

Nickolay
tnx for the tips.i'm no programmer. so most of that i just copy paste without fully understanding it.i just learn all these few hours ago.mediabox: http://mediabox.googlecode.com http://iaian7.com/webcode/mediaboxAdvancedand google hosted mootols: http://code.google.com/apis/ajaxlibs/documentation/index.html#mootoolsit works if i manually put in the rel tag. but i want to use js to put the rel tag
DennyHalim.com
No putting rel="" manually doesn't work: click nature1.jpg here http://jsbin.com/ihoji/edit . First try editing this to make it work without setting the attribute dynamically. Then you can update the commented-out code to set the correct attribute dynamically. Like I said it does what you wanted it to - sets rel="lightbox" on <a> elements.
Nickolay
while problem not (yet) solved, this very question is resolved. thank you all!
DennyHalim.com