PDA

View Full Version : [Solved] Spoiler Tag String Help...



derek barnstorm
08-10-2008, 09:30 PM
Hi,

I don't know if anyone is going to be able to help here or not... Sorry if there's not enough information.

I'm trying to put a spoiler tag into a .js file so it will display in a forum live preview, but it won't work and FireFox and IE just say there is a syntax error...

Anyway, here is the code:


ubbcstr=ubbcstr.replace(/\[spoiler\](.*?)\[\/spoiler\]/ig, "<div class='message'><div style='padding-bottom: 1px;'><b>Spoiler:</b> <input type='button' value='Show' onClick='if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = 'Hide'; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = 'Show'; }'></div><div class='spoiler'><div style='display: none;'>$1</div></div></div>");

Any help would be appreciated,

Thanks.

jscheuer1
08-11-2008, 03:15 AM
You can't do this:



onClick='if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = 'Hide'; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = 'Show'; }'

Basically what you have is a replacement string. It is delimited by double quotes ("), but contains representations of other string values delimited by single quotes ('). Within these, there are representations of string values delimited by single quotes as well. This wouldn't work if it were an actual string, and will not work here either.

Generally it is best to delimit your main string with the type of quote most natural to quoted strings within it, and then escape those where they appear within it, using the alternate (other type of) quote within nested strings.

Assuming that the rest of your code in the replacement string is OK, and that the rest of your code in general is also OK, this would work:


ubbcstr=ubbcstr.replace(/\[spoiler\](.*?)\[\/spoiler\]/ig, "<div class=\"message\"><div style=\"padding-bottom: 1px;\"><b>Spoiler:</b> <input type=\"button\" value=\"Show\" onclick=\"if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = 'Hide'; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = 'Show'; }\"></div><div class=\"spoiler\"><div style=\"display: none;\">$1</div></div></div>");

There could be other problems.

Dal
08-11-2008, 03:19 AM
well thats because your trying to get IE to work... theres your first problem.

Ive read up that IE8 is going to be 100% W3C compliant, Im not sure if its another MS lie but they have another EU case on their backs. IE8 should be complete before the year is up.

You can download the developer toolkit for IE7 to see how messed up your code gets. Expect your heart to sink when you see the tragerty of how bad IE messes with "YOUR" code.

I've give up on trying to work around it but there seems to be light at the end of the tunnel, just hope its not a train! ;)

Kind regards
Dal

derek barnstorm
08-11-2008, 04:02 AM
Thank you so much for that jscheuer1... It works perfectly, I wasn't expecting a solution like that... It was my fault for trying to get away with single quote rather than back slashing them then...

Thanks again!

Yeah Dal, I try to stay way from IE... I always dread testing my work on it...

Cheers!