HTML:
<!DOCTYPE html>
<html>
<head>
<title>Random Quote Generator</title>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<div class="container">
<h1>Random Quotes Rewritten</h1>
<p>Would you like a random sentence to send to someone or incorporate in your project? Press the button below!</p>
<div class="quotearea">
</div>
<button id="newquote">New quote</button>
<button class="tweetquote"><i class="fa fa-twitter" aria-hidden="true"></i>
Tweet Quote</button>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="script.js"></script>
</body>
</html>
JS:
var quoteObj;
var quoteContent;
var quoteTitle;
$("#newquote").click(function(){
$.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=30&jsonp=mycallback", function(a) {
$(".quotearea").empty();
$(".quotearea").addClass("border");
quote = a[Math.floor(Math.random()*a.length)];
quoteContent = quote.content;
quoteTitle = quote.title;
$(".quotearea").append(quoteContent + " " + quoteTitle);
$('.tweetquote:hidden').show();
});
});
$('.tweetquote').click(function() {
window.open('https://twitter.com/intent/tweet?text=' + encodeURIComponent(quoteContent) + (encodeURIComponent(quoteTitle)));
});
Using encodeURIComponent:
-
The quote:
-
The result:
Without encodeURIComponent:
-
The quote:
-
The result:
As you can see, when it hits the first apostrophe (or dash, etc.) the string cuts. I can understand why, itâs because itâs not being escaped. However using the URI Encode makes the string look funny and coverts the characters to &sdf4; (or whatever the heck it is).
Any help would be fantastic.
Thanks,
James.