Close image when clicked on outside $('html').click(); conflict with open image? - javascript

I have used a simple css and js for showing an image as popup when an image is clicked used and close button on top to close the image. Both openimage and closeimage are js functions
i have implemented as
open image function as
function imgv(x,y,img)
{
document.getElementById('imgv_i2').src = img;
document.getElementById('imgv_i2').style.width = x;
document.getElementById('imgv_i2').style.height = y;
document.getElementById('imgv_i2').width = x;
document.getElementById('imgv_i2').height = y;
imgToMiddle('imgv_i2');
document.getElementById('imgv_i2').style.display = 'block';
}
close image as
function closeimgv()
{
document.getElementById('imgv_i2').style.display = 'none';
document.getElementById('imgv_close').style.display = 'none';
}
event as
<img src="image src">
now for closing image when clicked on outside i have used these like
$('#imgv_i2').click(function(event){
event.stopPropagation();
});
$('html').click(function() {
if(document.getElementById('imgv_i2').style.display == 'block') {
closeimgv();
}
});
now what happens is when click for opening image triggered also the close image function also triggered how can i make the $('html').click(); bind after the open image function triggered and unbind after close image function. Thanks.

Try
$(document).click(function(ev) {
//avoid clicks on image itself
if($(ev.originalTarget).is("whatever triggers image opening")) {return;}
if(document.getElementById('imgv_i2').style.display == 'block') {
closeimgv();
}
});

I would recommend you to use jquery plugins for thick box or popup as they are very simple and easy to implement and customize. There are lots of open source plugins available try them out and they do provide the type of customization you are asking for !

Related

HTML buttons will only work only on webpage re/-load

EDIT : Thanks to everyone who tried to help me. I appreciate the tips guys.
I changed my window.onloadand inserted the two event listeners inside of it.
After that I took the idea of #Ito Pizarro , and implemented it in my own way.
The result looks like this :
function openDoor() {
var x_1 = document.getElementById('img1');
var x_2 = document.getElementById('img2');
is_visible = (x_1.style.visibility == "hidden");
if (is_visible) {
x_1.style.visibility = "visible";
}
else {
x_2.style.visibility = "hidden";
}}
And I also did the same for my closeDoor() function.
END OF EDIT
I create a HTML page, with two buttons. Every button has its purpose when it's being clicked. The first one will show an image of an opened door. The second button will show an image of a close door. When the page is loaded no image is being shown. They appear only if their button is clicked.
Tried to created a nested if-statement with the a global bool that will make it run infinitely.
Also tried a for & while loop.
But I am new to programming and I struggle a bit.
window.onload = function () {
document.getElementById('OpenDoor').addEventListener("click", function () { openDoor() })
}
window.onload = function () {
document.getElementById('CloseDoor').addEventListener("click", function () { closeDoor() })
}
function openDoor(){
document.getElementById('img1').style.visibility = "visible";
}
function closeDoor(){
document.getElementById('img2').style.visibility = "visible";
}
In the code exist two problems :
I load the page and click the "close door" button and the closed door image appears. If I decide to open the door again by pressing the "open door" button, it wont do it.
I load the page and click the "open door" button first. The open door image appears and the if I click on the "close door" button and the image also appears, but I cant repeat the process by re-clicking the "open door" to reopen it.
You are assining a function on the onload event twice. By doing this the first delaration will never be triggered.
It should be more something like :
window.onload = function () {
document.getElementById('OpenDoor').addEventListener("click", function () { openDoor() })
document.getElementById('CloseDoor').addEventListener("click", function () { closeDoor() })
}
Don't forget to validate the answer if you have what you were looking for
EDIT: In addition to lucien-dulac's point about window.onload…
It looks like your two event handlers do a single thing to either of two separate elements.
openDoor() will only ever make #img1 visible.
closeDoor() will only ever make #img2 visible.
If you want subsequent clicks on #OpenDoor or #CloseDoor to change the visibility style of their respective target elements — #OpenDoor controls #img1, #CloseDoor controls #img2 — you would need to write a toggle into openDoor() and closeDoor().
Something like…
function openDoor(){
var el = document.getElementById('img1'),
is_visible = ( el.style.visibility === "visible" );
if ( is_visible ) {
el.style.visibility = "hidden";
} else {
el.style.visibility = "visible";
}
}

IOS device not populating a click function attribute addition

For some reason when I click on a button that triggers a menu to open, an added attribute source does not populate the defined image with IOS mobile/tablets via a browser (I tried Chrome).
I searched around for a solution. I saw someone recommend adding "touchend" to my click function and I did, but it did not help. $('.requestButton').on('click touchend', function (event) {
I'm not sure how to demo this situation, since it is only happening on IOS mobile/tablet devices.
Click here to see it live
Scroll down to where you see the section below. Click on "Request Copy".
In the blue section, an image is supposed to be showing up. It shows up on my Android device and my desktop browser.
Does anyone know why this is not showing up with IOS devices?
var button_engagement = '';
$('.requestButton').on('click touchend', function (event) {
catalogChoice = $(this).data('catalog-name');
$('.catalogName').html(catalogChoice + ' Catalog');
event.stopPropagation();
$('#catalogPop').fadeIn(350);
$('body').css('overflow', 'hidden');
$('#formWrap').show();
$('#catalogSuccessWrap').hide();
$('[name="description').val(catalogChoice);
var requestButSel = $(this).data('catalog-name');
var availability = '';
//Populating the Request Block 1 Img
if (requestButSel == 'Profile') {
requestImgLink = 'link';
requestImgAlt = 'Alt';
} else if (requestButSel == 'Fastening Technology') {
requestImgLink = 'link';
requestImgAlt = 'Alt';
}
$('#requestBlock1img').attr('src', requestImgLink);
$('#requestBlock1img').attr('alt', requestImgAlt);
});

How to close popup when youtube video is playing on esc button button?

Click me to see the demo
When video is not playing then Esc button pressed the popup closes but it does not work when video is playing.
In your demo, ensure the iframe containing the video is cleared when the layer is hidden. Change your hide function to this:
function popup(){
var img = document.getElementsByClassName('image-bann')[0];
img.querySelector('iframe').src = "https://www.youtube.com/embed/0mQLTWvXbXM";
document.getElementsByClassName('pop')[0].style.display="block";
}
function hide(e) {
var img = document.getElementsByClassName('image-bann')[0];
if (img.contains(e.target)) {
e.preventDefault();
return false;
}
img.querySelector('iframe').src = "about:blank";
document.getElementsByClassName('pop')[0].style.display="none";
}

How can one <img> tag be used to execute 2 different js functions?

How can I set up one image, that when clicked changes to another image and then when clicked again reverts back to the original image while still carrying out functions independently. In my example I want a Play button that when pressed turns to a pause button. However I need to have both play and pause functionalities when the correct button is pressed. These work on do different buttons but I would like the one button to have all the functionality. I have tried a few things but everytime, one of the play/pause functions are not letting the other work.
$('#startSlider').click(function (){
if (document.getElementById("startSlider").src = "Play.png"){
document.getElementById("startSlider").src = "Pause.png";
scrollSlider();
}
else if (document.getElementById("startSlider").src != "Play.png"){
clearTimeout(tmOt);
document.getElementById("startSlider").src = "Play.png";
}
});
<img src="Play.png" id="startSlider"/>
problem is in your if block,
you are using = not ==
if (document.getElementById("startSlider").src = "Play.png"){// you are assigning here not comparing
you are assigning play.png every times when the click calls on the image.
you are using jquery then why are you not using this to make it more simple.
$('#startSlider').click(function (){
if (this.src == "play.png"){
this.src = "pause.png";
}else{
this.src = "play.png";
}
});
check this fiddle
I like using classes in these situations as it makes the code a little easier to follow:
$('#startSlider').click(function (){
$(this).toggleClass('pause')
if ( $(this).hasClass('pause') ) {
// Button is paused change to play
$(this).attr('src', 'Play.png')
// Pause function goes here
} else {
// Button is play change to pause
$(this).attr('src', 'Pause.png')
//play function goes here
}
})
You can do this with a little state machine. You keep the states inside an object, and handle the next state in the event listener of the button:
var states = {
_next: 'play',
next: function() {
return this[this._next]()
},
play: function() {
img.src = 'pause.jpg'
button.textContent = 'Pause'
this._next = 'pause'
},
pause: function() {
img.src = 'play.jpg'
button.textContent = 'Play'
this._next = 'play'
}
}
button.addEventListener('click', states.next.bind(states))
This is more of a general answer to your problem, that you'd have to adapt to your code.
DEMO: http://jsbin.com/moxoca/1/edit *
* The images might take a second to load.

Showing warning with timeout when opening external links

I want that when a user clicks on any external link (identified by either particular id or class) on my site then he should get a popup with a counter of 10 seconds, after 10 seconds the popup should close and the user should be able to access the external URL. How can this be done? I'm able to show a warning like below but I don't know how to add timeout to it, also this is a confirm box, not a popup where I can add some div and more stuff for user to see until the counter stops.
$(document).ready(function(){
var root = new RegExp(location.host);
$('a').each(function(){
if(root.test($(this).attr('href'))){
$(this).addClass('local');
}
else{
// a link that does not contain the current host
var url = $(this).attr('href');
if(url.length > 1)
{
$(this).addClass('external');
}
}
});
$('a.external').live('click', function(e){
e.preventDefault();
var answer = confirm("You are about to leave the website and view the content of an external website. We cannot be held responsible for the content of external websites.");
if (answer){
window.location = $(this).attr('href');
}
});
});
PS: Is there any free plugin for this?
I've put together a little demo to help you out. First thing to be aware of is your going to need to make use of the setTimeout function in JavaScript. Secondly, the confirmation boxes and alert windows will not give you the flexibility you need. So here's my HTML first I show a simple link and then created a popup div that will be hidden from the users view.
<a href='http://www.google.com'>Google</a>
<div id='popUp' style='display:none; border:1px solid black;'>
<span>You will be redirected in</span>
<span class='counter'>10</span>
<span>Seconds</span>
<button class='cancel'>Cancel</button>
</div>
Next I created an object that controls how the popup is displayed, and related events are handled within your popup. This mostly is done to keep my popup code in one place and all events centrally located within the object.
$('a').live('click', function(e){
e.preventDefault();
popUp.start(this);
});
$('.cancel').click(function()
{
popUp.cancel();
});
var popUp = (function()
{
var count = 10; //number of seconds to pause
var cancelled = false;
var start = function(caller)
{
$('#popUp').show();
timer(caller);
};
var timer = function(caller)
{
if(cancelled != true)
{
if(count == 0)
{
finished(caller);
}
else
{
count--;
$('.counter').html(count);
setTimeout(function()
{
timer(caller);
}, 1000);
}
}
};
var cancel = function()
{
cancelled = true;
$('#popUp').hide();
}
var finished = function(caller)
{
alert('Open window to ' + caller.href);
};
return {
start : start,
cancel: cancel
};
}());
If you run, you will see the popup is displayed and the countdown is properly counting down. There's still some tweaks of course that it needs, but you should be able to see the overall idea of whats being accomplished. Hope it helps!
JS Fiddle Sample: http://jsfiddle.net/u39cV/
You cannot using a confirm native dialog box as this kind of dialog, as alert(), is blocking all script execution. You have to use a cutomized dialog box non-blocking.
You can use for example: jquery UI dialog
Even this has modal option, this is not UI blocking.
Consdier using the javascript setTimeout function to execute an action after a given delay
if (answer){
setTimeOut(function(){
//action executed after the delay
window.location = $(this).attr('href');
}, 10000); //delay in ms
}

Categories

Resources