Last active
April 30, 2018 09:02
-
-
Save joshpalmeri/1b0d75bacca8c0c64becc2ebb58b23cb to your computer and use it in GitHub Desktop.
URL Parameter Auto Play on Page Load
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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'); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Get the value of a URL parameter, and use that value to programmatically trigger functionality on the page.