|
5 | 5 | .each( function( index, question ) {
|
6 | 6 | var $question = $( question ),
|
7 | 7 | $button = $( '<button />' ),
|
8 |
| - $h3 = $( '<h3 />' ), |
9 |
| - id = $question.attr( 'id' ); |
10 |
| - |
11 |
| - // If there is no ID, create our own. |
12 |
| - if ( ! id ) { |
13 |
| - id = encodeURIComponent( $question.text().toLowerCase().trim() ); |
14 |
| - $question.attr( 'id', id ); |
15 |
| - } |
16 |
| - |
17 |
| - $button.attr( 'formaction', id ).on( 'click', function( event ) { |
18 |
| - event.preventDefault(); |
19 |
| - window.location.hash = id; |
20 |
| - } ); |
| 8 | + $h3 = $( '<h3 />' ); |
21 | 9 |
|
22 | 10 | $question.html( $h3.html( $button.text( $question.text() ) ) );
|
23 | 11 | } )
|
|
41 | 29 |
|
42 | 30 | window.scrollTo( 0, $question.offset().top - scrollPaddingTop );
|
43 | 31 | }
|
| 32 | + |
| 33 | + if ( $question.prop( 'id' ) ) { |
| 34 | + window.location.hash = $question.prop( 'id' ); |
| 35 | + } |
44 | 36 | } );
|
45 | 37 |
|
46 | 38 | if ( window.location.hash ) {
|
47 |
| - jQuery( |
| 39 | + var uriHash = window.location.hash.substr(1), |
| 40 | + uriElement = document.getElementById( uriHash ); |
| 41 | + |
| 42 | + if ( ! uriElement ) { |
48 | 43 | // Decode/Encode here is to work with any existing links that are not fully-encoded, the trim handles whitespace/newlines.
|
49 |
| - document.getElementById( encodeURIComponent( decodeURIComponent( window.location.hash.substr(1) ).trim() ) ) |
50 |
| - ).trigger( 'click' ); |
| 44 | + uriHash = encodeURIComponent( decodeURIComponent( uriHash ) ).trim(); |
| 45 | + uriElement = document.getElementById( uriHash ); |
| 46 | + |
| 47 | + if ( ! uriElement ) { |
| 48 | + // Perform PHP-style URI encoding. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI#encoding_for_rfc3986 |
| 49 | + uriHash = uriHash.replace( /[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}` ); |
| 50 | + uriElement = document.getElementById( uriHash ); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + jQuery( uriElement ).trigger( 'click' ); |
51 | 55 | }
|
52 | 56 | } )( jQuery );
|
0 commit comments