0

I have created a Page in SharePoint online and it contains an embed code from another service that we utilize. I want this service to track the user that viewed the page within the embed. To do this, I need to add "?id=usersemailaddress" to the URL AFTER the user has launched the page. I am using the code below, but it fails at window.history.pushstate. Any help would be appreciated.

$(function() {
  document.title = "Custom Title";
  $( "#ctl00_fullscreenmodeBtn" ).hide();
});
SP.SOD.executeFunc('sp.js', 'SP.ClientContext',function(){  
  var context = new SP.ClientContext.get_current();
  var web = context.get_web();
  var currentUser = web.get_currentUser();
  context.load(currentUser);
  context.executeQueryAsync(
  function(){
    console.log(currentUser.get_email()); 
      if (window.location.href  === "https://example.sharepoint.com/sites/test/SitePages/test.aspx") {
         window.history.pushState("object or string", "Title", "https://example.sharepoint.com/sites/test/SitePages/test.aspx?id=" + currentUser.get_email());
      }
    }, 
  function(sender, args){
    console.log('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
  });
});
  • wouldn't you just do a window.location.href = "https://example.sharepoint.com/sites/test/SitePages/test.aspx?id=" + currentUser.get_email()); – Eric Alexander Sep 2 '15 at 2:42
1

you should try this :

var url = window.location.href;    
if (url.indexOf('?') > -1){
   url += '&param=1'
}else{
   url += '?param=1'
}
window.location.href = url;
  • I've tried this and it isn't quite working. I added a console.log(url) inside the else statement (which is the one that will fire almost 100% of the time for my scenario) and it logs the updated url with my parameter added properly. However, the page does not reload with this parameter. – Wyatt Sep 2 '15 at 13:06
  • can you please mention what "window.location.href" returns ? – Pranav Patel Sep 3 '15 at 4:41
  • After much trial and error, I figured out that the embed code I was using with the page had to be in a script web part BELOW the code I was using to reload the page. Something in the vendors embed code was causing a javascript error that halted the reload. – Wyatt Sep 4 '15 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.