Skip to content

Instantly share code, notes, and snippets.

@joshpalmeri
Last active April 30, 2018 09:02
Show Gist options
  • Save joshpalmeri/1b0d75bacca8c0c64becc2ebb58b23cb to your computer and use it in GitHub Desktop.
Save joshpalmeri/1b0d75bacca8c0c64becc2ebb58b23cb to your computer and use it in GitHub Desktop.
URL Parameter Auto Play on Page Load
/* From http://www.jquery4u.com/snippets/url-parameters-jquery/ */
/* Usage:
$('#city').val(decodeURIComponent($.urlParam('city')));
*/
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results==null){
return null;
}
else{
return results[1] || 0;
}
};
/* Update URL Param */
/* Josh Palmeri, 1/29/2018 */
/* Usage:
$.updateURL('play','11234');
*/
$.updateURL = function(param,val) {
if (history.pushState) {
var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?'+param+'='+val;
window.history.pushState({path:newurl},'',newurl);
}
}
// Programmatically update the browser URL on the desired listener
$('.my-element').on('click touchstart', function(){
$.updateURL('play',thisID);
}
// Get URL param on page load and trigger the video play
$(document).ready(function(){
var playParam = $.urlParam('play');
if(playParam!=''&&playParam!=null&&playParam!='undefined'&&playParam!=undefined) {
var playID = '#'+playParam;
$(playID).trigger('click');
}
});
@joshpalmeri
Copy link
Author

Get the value of a URL parameter, and use that value to programmatically trigger functionality on the page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment