Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Apostrphe's Breaking my Code 1

Status
Not open for further replies.

alphacooler

Programmer
Aug 29, 2005
73
US
I am working with Blogger BETA to add links at the end of every post (ala Digg or Delicious style), but the problem is that before I can encodeURIComponent the text, the apostrophe's from the post I am storing into a variable break it.

var title = &#39;<data:post.title/>&#39;;
title = encodeURIComponent(title);

var title = 'It's yet another test post.';
title = encodeURIComponent(title);

As you can see in #2 it is broken. How would I be able to handle single and double quotes?
 
Personally, I'd use single quotes as your string delimiters, and escape single quotes inside the string with a \ character:

Code:
var stringVar = 'It\'s yet another test " post with \' some quotes " in it';

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Yes, but in the example I provided, <data:post.title/> is blogger's own variable which they output directly into the JS. So I wouldn't be able to escape it because it is printed out as a string in my JS before I can do anything to it.

 
Hope this helps.
Code:
<script type="text/javascript">
<!--//
function escape_quotes(str) {
    str = str.replace( "'", "\'" );
    return str;
}

// note the location
var title = escape_quotes("<data:post.title/>");
alert(title);
//-->
</script>

M. Brooks
 
mbrooks.

Works for apostrophes, but what about for quotes? Posts without " " in them are fine now(thanks to you!). But if they have quotes, I get errors.
 
just add another replace function:
Code:
<script type="text/javascript">
<!--//
function escape_quotes(str) {
    str = str.replace( "'", "\'" )[!].replace('"', '\"')[/!];
    return str;
}

// note the location
var title = escape_quotes("<data:post.title/>");
alert(title);
//-->
</script>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Tried that.

Here is what the source looks like:

<script type='text/javascript'>

function escape_quotes(str) {
str = str.replace( "'", "\'" ).replace('"', '\"');
return str;
}

// note the location
var title = escape_quotes("It's yet antoher test");

var url = '
var full_body = escape_quotes("So let's see how it handles <b>tags</b> <img src="image.gif" />");
full_body = full_body.replace(/(<([^>]+)>)/ig,"");

blog_title = escape_quotes("My blog Title");

var link = ' + url + '&title=' + title + '&blog_title=' + blog_title + '&body=' + full_body;

document.write('<a href="' + link + '" target="_blank">Click This</a>');

</script>
 
Yeah, actually none of the solutions have worked. I thought the mbrooks worked, but it was only because he used "" instead of '' for the strings.

Any ideas?
 
Code:
<script type="text/javascript">
<!--//
function escape_quotes(str) {
    str.replace( "'", "\'" );
    str.replace( '"', '\"' );
    return str;
}

// note the location
var title = escape_quotes("<data:post.title/>");
alert(title);
//-->
</script>

M. Brooks
 
Ok. Here is my contribution to solving this problem. It's a total hack but it will work though.
Code:
<html>

<!-- note the location -->

<div id="quotes_hell" style="display:none">
	These "quotes" are driving me 'insane'
</div>

<script type="text/javascript">
<!--//
function escape_quotes(str) {
    str = str.replace( "'", "\'" );
    return str;
}

// note the location
var text = document.getElementById("quotes_hell").innerHTML;
alert( escape_quotes(text) );
//-->
</script>

</html>

M. Brooks
 
I've been researching all day and have come up empty. Anyone have any thoughts?
 
Unless there is a javascript replacement for " and ', I don't think it's possible without altering the actual script which outputs <data:post.title/>.

In Perl you would solve this situation like so
Code:
my $var = qq{ These "quotes" are driving me 'insane' };

or 

my $var = qq| These "quotes" are driving me 'insane' |;

or using a heredoc.

M. Brooks
 
Your hacked solution worked, but of course it isn't too pretty. I've looked around and it seems nobody has code that is working for Digg type Links. They will chunk on apostrophes.

One strange thing is that blogger beta keeps converting my double quotes into single quotes. I think it has something to do with there XML validation script.

I'm not sure if you have a blogger account, but if it isn't too much to ask (and it just might be), it only takes about 30 seconds to sign up for a BETA account ( Then you just go to:

"template" > "edit html" > "expand Widget Templates"

And then just ctrl+F for "post-labels" and add in any test code. <data:post.title/> is a tag that will output the title.

Perhaps maybe someone else who has blogger could give it a go?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top