Skip to content

Instantly share code, notes, and snippets.

@MrDys
Created August 29, 2012 13:26
Show Gist options
  • Select an option

  • Save MrDys/3512455 to your computer and use it in GitHub Desktop.

Select an option

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.
*/
@miketif

miketif commented Nov 10, 2015

Copy link
Copy Markdown

Thank you for this.

@ercanertan

Copy link
Copy Markdown

What about if you need to send parameters ? http://www.website.com/page.html#myModal?id=123

@MaartenW

Copy link
Copy Markdown

@ercanertan, send parameters where? You are now sending parameters to the client side. If you need to send the parameters to the server side you would need to place the parameters before the anchor, like:
http://www.website.com/page.html?id=123#myModal

@helms-charity

Copy link
Copy Markdown

This is great, thanks!

@webhacking

Copy link
Copy Markdown

👍

@woppo

woppo commented Jun 1, 2016

Copy link
Copy Markdown

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
Copy Markdown

Hi. Thank you

@mrky007

mrky007 commented Jun 20, 2016

Copy link
Copy Markdown

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

@iggymakesthings

Copy link
Copy Markdown

thanks a lot

@awanzse

awanzse commented Sep 13, 2016

Copy link
Copy Markdown

thx bro. 👍

@fabioonet

Copy link
Copy Markdown

Thanks

@deneshgautam

Copy link
Copy Markdown

where i see a demo??

@ravijoon

ravijoon commented Apr 6, 2017

Copy link
Copy Markdown

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
Copy Markdown

Thanks :)

@conchoecia

Copy link
Copy Markdown

Rad

@monogios

Copy link
Copy Markdown

Thanks @ravijoon

@mjawaids

Copy link
Copy Markdown

Awesome. This made my day.

@encompass

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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

@InnoM

InnoM commented Mar 19, 2019

Copy link
Copy Markdown

Thank you! you helped a lot

@bradley-varol

bradley-varol commented Jun 19, 2019

Copy link
Copy Markdown

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
Copy Markdown

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

@callumoberholzer

Copy link
Copy Markdown

Thanks for this, great fix!

@banagale

Copy link
Copy Markdown

Thanks Bradley-varol for this useful snippet.

@travisblock

Copy link
Copy Markdown

thanks

@nmansuri191

Copy link
Copy Markdown

thanks

@lepastiche

Copy link
Copy Markdown

OMG! Thanks a lot!

@intelligentpotato

intelligentpotato commented Jan 7, 2023

Copy link
Copy Markdown

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

@xaledzebari

Copy link
Copy Markdown

Thats cool bro , thanks

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