Jackson Cunningham
Jackson Cunningham

Reputation: 5073

Javascript query string not working on mobile

<script>// <![CDATA[
$(function(){
  var currentPostUrl = window.location.href + "?ref=blogshare"
  var currentPostTitle = $('.post-title').text().trim();

  $('#myModal').appendTo("body") 

  var facebookShareUrl = "https://www.facebook.com/sharer/sharer.php?u=" + currentPostUrl
  var emailShareUrl = "mailto:?to=&body=Hey, I thought you might like this post. It's called " + currentPostTitle + ". You can read it here:  " + currentPostUrl + "&subject=Thought you might like this post..."


  $('.email-share').attr('href', emailShareUrl);

// ]]></script>

The goal of the above script is to get the current url and append a ref query string to it. Then we change the href of a link to add a custom mailto.

The above seems to work perfectly fine on desktop. But on mobile (chrome iphone), when I click the email-share link, the ?ref=blogshare is not being appended to the link.

Any ideas why?

Upvotes: 0

Views: 285

Answers (1)

epascarello
epascarello

Reputation: 207527

You need to encode querystring values.

encodeURIComponent()

var currentPostUrl = encodeURIComponent(window.location.href + "?ref=blogshare");

Upvotes: 1

Related Questions