Bacho sh Bacho sh - 13 days ago 8
Javascript Question

error on window.location.hash

i have rails app where i modify some files and then run bundle instal and assets:precompile.

after that i have error in file wich was not changed.
part of file where error happened:

<script type="text/javascript">
function openCurrentTab() {
var hash = window.location.hash || '#info'
$('a[href=' + hash + ']').tab('show')
}

$(function() {
openCurrentTab()

$('#outage-tabs > li > a').click(function(evt) {
evt.preventDefault()
$(this).tab('show')
history.pushState(null, null, this.hash)
})
})
window.onpopstate = openCurrentTab
</script>


error (in console)

application-76c7fea….js:24 Uncaught Error: Syntax error, unrecognized expression: a[href=#info]


thank for help

Answer

Seems you have errors in your jQuery search

$('a[href=' + hash + ']').tab('show')

Use end-with selector and next syntax: do not use # inside term, use '' for setting search term

$("a[href$='hash-without-#-symbol']")

https://api.jquery.com/attribute-ends-with-selector/