473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Close popup image if detects mouse click

11
I am a beginner with very basic knowledge of Javascript.

Please help me to modify this script. When somebody clicks on the thumbnail to open up the enlarged popup image ( not an html page).
I have seen somewhere where somebody tries to left click on the popup image or right click to save it, the popup closes by it self.
Self close timer won't for me. I tried watermarking for the images. People wipe the watermark and still use them as if their own. :(

Expand|Select|Wrap|Line Numbers
  1.  
  2. <!-- ;
  3. var newwindow = ''
  4. function popitup(url) {
  5.     if (newwindow.location && !newwindow.closed) 
  6.     { newwindow.location.href = url; newwindow.focus(); } 
  7. else 
  8.     { newwindow=window.open(url,'htmlname','width=500,height=600,resizable=1') ; }
  9. }
  10. function tidy()
  11. {
  12. if (newwindow.location && !newwindow.closed) { newwindow.close(); } 
  13. }
  14. // -->
  15. </script>
  16.  
  17. <a href="javascript:popitup('image_to_open_in_popup.jpg')"><img src="image_from_the_html_page.jpg" width="300" height="225" border="0" alt="image_name"></a>
Jul 26 '07 #1
20 3349
pbmods
5,821 Expert 4TB
Heya, bebu. Welcome to TSDN!

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

Try this:

Expand|Select|Wrap|Line Numbers
  1. window.onclick = window.close;
  2.  
Jul 26 '07 #2
bebu
11
Thanks for your reply! Where should I add above code?
Jul 26 '07 #3
pbmods
5,821 Expert 4TB
Heya, bebu.

Thanks for your reply! Where should I add above code?
You'll want to put that in the code of the pop-up window, preferably in the HEAD element, since that way the User can't click the stop button to bypass your code.
Jul 27 '07 #4
bebu
11
Popup window opens only image. There is no head content or HTML document
Jul 27 '07 #5
pbmods
5,821 Expert 4TB
Heya, bebu.

Popup window opens only image. There is no head content or HTML document
Fair enough. In that case, you'll add a slightly different line of code right after you open the window:
Expand|Select|Wrap|Line Numbers
  1. newwindow.onclick = newwindow.close;
  2.  
Where newwindow is the variable that references the newly-opened window.
Jul 27 '07 #6
bebu
11
Heya, bebu.



Fair enough. In that case, you'll add a slightly different line of code right after you open the window:
Expand|Select|Wrap|Line Numbers
  1. newwindow.onclick = newwindow.close;
  2.  
Where newwindow is the variable that references the newly-opened window.
Where do I insert the above code? I tried, may be I am doing something wrong. I have very little knowledge of Javascript.

Thanks pbmods!
Jul 27 '07 #7
pbmods
5,821 Expert 4TB
Heya, bebu.

Ok. So we have here on line 7 of your OP:
Expand|Select|Wrap|Line Numbers
  1. newwindow=window.open(url,'htmlname','width=500,he  ight=600,resizable=1')
  2.  
This opens a new window and 'stores' it in the newwindow variable. So far so good.

Now the goal is to set this up so that whenever the User clicks anywhere in that window, the window closes.

To do this, we'll set up an event handler that triggers when the User clicks the mouse in that window, also known as:
Expand|Select|Wrap|Line Numbers
  1. newwindow.onclick
  2.  
When the User clicks in the window, we want to close it. Fortunately, there is a function that does exactly that:
Expand|Select|Wrap|Line Numbers
  1. newwindow.close()
  2.  
Now the trick is, we don't want to call newwindow.close() UNTIL the User clicks in that window (in other words, we don't want to call it until the onclick event fires for the new window).

So we have to chop off the '()' from the end:
Expand|Select|Wrap|Line Numbers
  1. newwindow.onclick = newwindow.close;
  2.  
I wrote an article about why the '()' has to get removed from the end of the function name, but you don't have to worry about that right now. For now, just trust me that you have to do this.

So we now have our statement that will set up newwindow to close whenever the User clicks in it. But where do we put it?

The best place for it is directly after we create the new window:
Expand|Select|Wrap|Line Numbers
  1. if (newwindow.location && !newwindow.closed)
  2. {
  3.     newwindow.location.href = url; newwindow.focus();
  4. }
  5. else
  6. {
  7.     newwindow = window.open(url,'htmlname','width=500,height=600,resizable=1') ;
  8.     newwindow.onclick = newwindow.close;
  9. }
  10.  
Jul 28 '07 #8
bebu
11
:

Expand|Select|Wrap|Line Numbers
  1. if (newwindow.location && !newwindow.closed)
  2. {
  3.     newwindow.location.href = url; newwindow.focus();
  4. }
  5. else
  6. {
  7.     newwindow = window.open(url,'htmlname','width=500,height=600,resizable=1') ;
  8.     newwindow.onclick = newwindow.close;
  9. }
  10.  


Hi pbmods,

It works in Firefox/2.0.0.5 but not in IE 6.0.2. Why is that?
Jul 28 '07 #9
pbmods
5,821 Expert 4TB
Heya, bebu.

Hi pbmods,

It works in Firefox/2.0.0.5 but not in IE 6.0.2. Why is that?
Dunno. I have a Mac.

Does it give you an error?
Jul 28 '07 #10
bebu
11
Heya, bebu.



Dunno. I have a Mac.

Does it give you an error?
It doesn't give me any error. It closes with X but not with right or left click. And works fine in FireFox.

Am I allowed to post a link for the test page?
Jul 28 '07 #11
pbmods
5,821 Expert 4TB
Heya, bebu.

Am I allowed to post a link for the test page?
Sure. The rule against external links only applies to spam or commercial links. You are always welcome to post links to test pages and articles that help explain your posts.
Jul 28 '07 #12
bebu
11
Heya, bebu.



Sure. The rule against external links only applies to spam or commercial links. You are always welcome to post links to test pages and articles that help explain your posts.

Thanks pbmods!

Here is link ...
Test page
Jul 28 '07 #13
pbmods
5,821 Expert 4TB
Heya, Bebu.

Your page works as expected in Firefox on the Mac. However, it does not work in Safari, which would lead me to expect that this is a Firefox-only feature.

Here's what I would do. Instead of using an image as the URL for the pop-up, use an HTML document instead.

popup_image1.html:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     .
  3.     .
  4.     .
  5.  
  6.     <body onclick="window.close();">
  7.         <img ... />
  8.         .
  9.         .
  10.         .
  11.     </body>
  12. </html>
  13.  
And then in the original page:
Expand|Select|Wrap|Line Numbers
  1. newwindow = window.open('popup_image1.html' ... );
  2.  
Jul 28 '07 #14
bebu
11
Heya, Bebu.

Your page works as expected in Firefox on the Mac. However, it does not work in Safari, which would lead me to expect that this is a Firefox-only feature.

Here's what I would do. Instead of using an image as the URL for the pop-up, use an HTML document instead.

popup_image1.html:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     .
  3.     .
  4.     .
  5.  
  6.     <body onclick="window.close();">
  7.         <img ... />
  8.         .
  9.         .
  10.         .
  11.     </body>
  12. </html>
  13.  
And then in the original page:
Expand|Select|Wrap|Line Numbers
  1. newwindow = window.open('popup_image1.html' ... );
  2.  

I would like to avoid making HTML documents for the images, since I have over 150 images which has popup code.
Jul 29 '07 #15
pbmods
5,821 Expert 4TB
Heya, Bebu.

I would like to avoid making HTML documents for the images, since I have over 150 images which has popup code.
Will you be able to execute server-side scripts such as .php or .asp on your server?
Jul 29 '07 #16
bebu
11
Heya, Bebu.



Will you be able to execute server-side scripts such as .php or .asp on your server?

I have no idea. My site is with two hosting companies. I will have to find out that on Monday. I have very limited knowledge of computer scripts or languages. I am an artist and I do maintain my website.
Jul 29 '07 #17
kovik
1,044 Expert 1GB
So you know, the window.open() function returns a reference to the 'window' object of the new window, which is the highest element of the DOM. You can alter this object in any way that you want to, including generating the new page dynamically. Of course, I prefer that this kind of thing is done server-side, but it is perfectly valid client-side as well.

Since you are loading only the image, you may think that the image is the only object in the DOM on that page. You'd be incorrect in that assumption, though. The <html> and <body> elements still exist, as well as a single <img> element. Since it seems as though setting an onclick event for the window object isn't cross-browser compatible, set the event to the body element.

Expand|Select|Wrap|Line Numbers
  1. function popitup(url)
  2. {
  3.     var newwindow = window.open(url, 'imageWindow', 'width=500,height=600,resizable=1');
  4.  
  5.     newwindow.document.body.onclick = newwindow.close();
  6. }
Try it.

Also, FYI, you don't need the if statement in your popitup() function. The open() function automatically overwrites windows with the same name.
Jul 30 '07 #18
bebu
11
So you know, the window.open() function returns a reference to the 'window' object of the new window, which is the highest element of the DOM. You can alter this object in any way that you want to, including generating the new page dynamically. Of course, I prefer that this kind of thing is done server-side, but it is perfectly valid client-side as well.

Since you are loading only the image, you may think that the image is the only object in the DOM on that page. You'd be incorrect in that assumption, though. The <html> and <body> elements still exist, as well as a single <img> element. Since it seems as though setting an onclick event for the window object isn't cross-browser compatible, set the event to the body element.

Expand|Select|Wrap|Line Numbers
  1. function popitup(url)
  2. {
  3.     var newwindow = window.open(url, 'imageWindow', 'width=500,height=600,resizable=1');
  4.  
  5.     newwindow.document.body.onclick = newwindow.close();
  6. }
Try it.

Also, FYI, you don't need the if statement in your popitup() function. The open() function automatically overwrites windows with the same name.
It's not working. When I click the link it opens up the popup. When I click on the pop up it does not close. If I click another link from the parent window, then the popup closes.
Jul 30 '07 #19
bebu
11
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3. // JK Pop up image viewer script- By JavaScriptKit.com
  4. // Visit JavaScript Kit (http://javascriptkit.com)
  5. // for free JavaScript tutorials and scripts
  6. // This notice must stay intact for use
  7.  
  8. var popbackground="black" //specify backcolor or background image for pop window
  9. var windowtitle="Larger View" //pop window title
  10.  
  11. function detectexist(obj){
  12. return (typeof obj !="undefined")
  13. }
  14.  
  15. function jkpopimage(imgpath, popwidth, popheight, textdescription){
  16.  
  17. function getpos(){
  18. leftpos=(detectexist(window.screenLeft))? screenLeft+document.body.clientWidth/2-popwidth/2 : detectexist(window.screenX)? screenX+innerWidth/2-popwidth/2 : 0
  19. toppos=(detectexist(window.screenTop))? screenTop+document.body.clientHeight/2-popheight/2 : detectexist(window.screenY)? screenY+innerHeight/2-popheight/2 : 0
  20. if (window.opera){
  21. leftpos-=screenLeft
  22. toppos-=screenTop
  23. }
  24. }
  25.  
  26. getpos()
  27. var winattributes='width='+popwidth+',height='+popheight+',resizable=no,left='+leftpos+',top='+toppos
  28. var bodyattribute=(popbackground.indexOf(".")!=-1)? 'background="'+popbackground+'"' : 'bgcolor="'+popbackground+'"'
  29. if (typeof jkpopwin=="undefined" || jkpopwin.closed)
  30. jkpopwin=window.open("","",winattributes)
  31. else{
  32. getpos() //uncomment these 2 lines if you wish subsequent popups to be centered too
  33. jkpopwin.moveTo(leftpos, toppos)
  34. jkpopwin.resizeTo(popwidth, popheight+30)
  35. }
  36. jkpopwin.document.open()
  37. jkpopwin.document.write('<html><title>'+windowtitle+'</title><body '+bodyattribute+'><img src="'+imgpath+'" style="margin-bottom: 0.5em" onBlur="self.close()" onClick="self.close()" onContextmenu="return false;"><meta http-equiv="imagetoolbar" content="no"><br>'+textdescription+'</body></html>')
  38. jkpopwin.document.close()
  39. jkpopwin.focus()
  40. }
  41. </script>
The above script works fine as long as you only click links for the popups.
It closes the popup when someone clicks on it.

Only thing is not working with this script is if page A has all the links for the popups. After one popup is open and a person clicks on the link from the Page A which opens the page B and decides to come back to page A and starts clicking on the the popup links. Then the popup won't come in front. If we bring it to front or close the popup then the popup functions stops working. Even after closing the popup new popup doesn't come.
Jul 30 '07 #20
kovik
1,044 Expert 1GB
It's not working. When I click the link it opens up the popup. When I click on the pop up it does not close. If I click another link from the parent window, then the popup closes.
Oh well. It was just theory, so I'd never tested it. All of the elements exist, but I guess they don't accept JavaScript. You should really use a document instead of just the image if you want to do what you're after if you can't alter the DOM.
Jul 31 '07 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Csaba2000 | last post by:
I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE 5.5+). This should work regardless of whether IE has focus. Normally you would...
8
by: Pal Csepregi | last post by:
Can i close a windows without the question that do i want to close it? Pali
17
by: Applebrownbetty | last post by:
Hi, I'd like to append/amend the following code from the Dreamweaver extension "Open Picture Window Fever" for the ability to center the PopUp window: By default, it allows the window to be...
4
by: Colin Graham | last post by:
Hi guys, Just a quickie here that i hope someone can help me with. Basically i want stop the user from closing the popup window using the small x button in the top right hand corner. Im aware...
24
by: jonathon | last post by:
Hi all, I have a web app with a popup window for entering data. I don't want to access the web every time this window is opened, as most of the app is AJAX. But I can't figure out how to open...
3
by: EnjoyNews | last post by:
I have a popup problem. I have a script that generates a popup for image viewing. It has 2 function, and the first is that it automaticly generates the popup window to the size of the image,...
12
by: Yuaw | last post by:
I managed to cobble together some code and all I need to be on my way is to have the close button I created to always be at the top right of my pop up. The pop ups are large versions of thumbnail...
1
by: johnix | last post by:
i am a bigenner in this field of web programming.. i like to create a dynamic dropdown menu that the data should came from the database but i dont know how to do it. what i did is I use the table and...
5
by: SQACSharp | last post by:
I'm trying to automate an action in Internet Explorer.... I'm trying to click an image that open a popup menu. The html code of the iimage look like like <img blabla..OnClick="ShowMenu()"/> I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.