Skip to content

Instantly share code, notes, and snippets.

@MrDys
Created August 29, 2012 13:26
Show Gist options
  • Star 91 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save MrDys/3512455 to your computer and use it in GitHub Desktop.
Save MrDys/3512455 to your computer and use it in GitHub Desktop.
Link directly to an open modal window in Bootstrap
/* If you've ever had the need to link directly to an open modal window with Bootstrap, here's a quick and easy way to do it:
Make sure your modal has an id:
<div class="modal" id="myModal" ... >
Then stick this bit of Javascript at at the end of your document:
*/
$(document).ready(function() {
if(window.location.href.indexOf('#myModal') != -1) {
$('#myModal').modal('show');
}
});
/*
Then you can send people to http://www.website.com/page.html#myModal and it'll load the page with the modal open.
*/
@woppo
Copy link

woppo commented Jun 1, 2016

Hello, thanks for the code.

i have a newbie questione for you:
how do i make it work in wordpress? where do i have to put this code?

thanks a lot

@hamedinia
Copy link

Hi. Thank you

@mrky007
Copy link

mrky007 commented Jun 20, 2016

I owe you a beer for this one. Saved me a lot of time. Thanks !!!

@iggymakesthings
Copy link

thanks a lot

@awanzse
Copy link

awanzse commented Sep 13, 2016

thx bro. 👍

@fabioonet
Copy link

Thanks

@deneshgautam
Copy link

where i see a demo??

@ravijoon
Copy link

ravijoon commented Apr 6, 2017

Read it somewhere, we do like this... see if it helps

if (window.location.hash && $(window.location.hash).length) {
  $(window.location.hash).modal('show');
}

@MAPReiff
Copy link

Thanks :)

@conchoecia
Copy link

Rad

@monogios
Copy link

Thanks @ravijoon

@mjawaids
Copy link

Awesome. This made my day.

@encompass
Copy link

First off thank you, it is what I am actually wanting to do. However, I can only get the modal to show when it is a refresh, if it is a freshly typed link it doesn't seem to work. This is Bootstrap 3.3x with Firefox.

@miguelangelmagdalena
Copy link

Thx.
In case you have more modals, I'm using something like this:

$(document).ready(function(){

	//Open modal con url
	var url      	= window.location.href;
	var modal_code 	= getParameterByName("modal",url);

	if(window.location.href.indexOf('?modal='+modal_code) != -1) {
    	$('#myModal'+modal_code).modal('show');
    	/*Like a query string
		http://www.website.com/page.html?modal=1
		http://www.website.com/page.html?modal=2
		...
    	*/
  	}
	
}); 

function getParameterByName(name, url) { //Obtiene un value de un query string
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));

    /*EJEMPLO
	// query string: ?foo=lorem&bar=&baz
	var foo = getParameterByName('foo'); // "lorem"
	var bar = getParameterByName('bar'); // "" (present with empty value)
	var baz = getParameterByName('baz'); // "" (present with no value)
	var qux = getParameterByName('qux'); // null (absent)
    */
}

@nicoleherold
Copy link

Thank you. I was looking the hole day for that.

@InnoM
Copy link

InnoM commented Mar 19, 2019

Thank you! you helped a lot

@bradley-varol
Copy link

bradley-varol commented Jun 19, 2019

This will work for all modals on your site. Just add the modal name to the url hash (https://example.com?page=1#myModal):

jQuery

$(document).ready(function () {
    $('.modal').each(function () {
        const modalId = `#${$(this).attr('id')}`;
        if (window.location.href.indexOf(modalId) !== -1) {
            $(modalId).modal('show');
        }
    });
});

ES6

const modals = [...document.getElementsByClassName('modal')];
modals.forEach((modal) => {
    const modalId = `#${modal.id}`;
    if (window.location.href.indexOf(modalId) !== -1) {
        $(modalId).modal('show');
    }
});

@AFlorencia
Copy link

Thank you very much!!! You saved my head today!

@callumoberholzer
Copy link

Thanks for this, great fix!

@banagale
Copy link

Thanks Bradley-varol for this useful snippet.

@travisblock
Copy link

thanks

@nmansuri191
Copy link

thanks

@lepastiche
Copy link

OMG! Thanks a lot!

@intelligentpotato
Copy link

intelligentpotato commented Jan 7, 2023

In 2022 you can do:
if (window.location.hash == '#myModal')

@xaledzebari
Copy link

Thats cool bro , thanks

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