Preloading image on next page in lightbox - html

I am using a lightbox in my react app (displays an image in a modal, user can go to next image by hitting right arrow key).
The problem is that whenever a user goes to the next image, it takes a second for the tag to make the request to load the image. I would like to pre-load the next image before the user gets to it.
All of the image URLs are fetched at once in the parent component, so I have all of the image urls.
Does anyone know of a way I can pre-load the image so it doesn't have to take the second to fetch and render?

I'm not exactly sure if I understood correctly what solution you would like, but you could use.
If you want to have a look: https://github.com/desandro/imagesloaded
which is specifically made for use cases like masonry plugin layouts, to check if all images have finished loading and then execute functions / other scripts.
Example:
$('#your-container').imagesLoaded( {
},
function() {
$('a.gallery').featherlight({
targetAttr: 'href'
});
}
);
I hope it helps a little.

Related

Stop people downloading images

Ok,
I have a script on my website that counts the amount of clicks and downloads each of the images on the site gets, but I have noticed that people have been right clicking and downloading the images, or dragging them to the top bar to get them without clicking on them.
Is there any way I can stop them from being able to get the image without clicking on it first? I have disabled right click already, but there is still a way to get the images (by dragging the image to the url bar).
Thanks
As far as I know, you can't. Because there is always a url in the source code.
You can prevent right-click with the below code if it helps. Also take a long at a previously asked question Disabling right click on images using jquery. You can use flash or overlay it with another transparent/blank image to make it difficult to copy.
$(function () {
$(this).bind("contextmenu", function (e) {
e.preventDefault();
});
});

Protect image download

I know the best way to protect image download is not putting it on internet in the first place.
I assume there is no 100% protection against image download and that if a user can see an image on internet he can with a bit of experience find access to download it.
I am aware of transparent .gif or .png covering the images or using background_image CSS property to protect it and prevent right click download but are there
other ways to complicate image download and therefore prevent image download by most users?
Here is simple code to start with :
<img src="http://placekitten.com/600/450">
Another way to remove the "save image" context menu is to use
some CSS. This also leaves the rest of the context-menu intact.
img {
pointer-events: none;
}
It makes all img elements non-reactive to any mouse events such as dragging, hovering, clicking etc.
See spec for more info.
In reactjs project, avobe code put in global CSS (index.css)
No there actually is no way to prevent a user from doing a particular task. But you can always take measures! The image sharing websites have a huge team of developers working day and night to create such an algorithm where you prevent user from saving the image files.
First way
Try this:
$('img').mousedown(function (e) {
if(e.button == 2) { // right click
return false; // do nothing!
}
});
So the user won't be able to click on the Save Image As... option from the menu and in turn he won't get a chance to save the image.
Second way
Other way is to use background-image. This way, the user won't be able to right click and Save the Image As... But he can still see the resources in the Inspector.
Third way
Even I am new to this one, few days ago I was surfing Flickr when I tried to right click, it did not let me do a thing. Which in turn was the first method that I provided you with. Then I tried to go and see the inspector, there I found nothing. Why? Since they were using background-image and at the same time they were using data:imagesource as its location.
Which was amazing for me too. You can precvent user from saving image files this way easily.
It is known as Data URI Scheme: http://en.wikipedia.org/wiki/Data_URI_scheme
Note
Please remember brother, when you're letting a user surf your website you're giving him READ permissions on the server side so he can read all the files without any problem. The same is the issue with image files. He can read the image files, and then he can easily save them. He downloads the images on the first place when he is surfing your website. So there is not an issue for him to save them on his disk.
If it is only image then JavaScript is not really necessary. Try using this in your html file :
<img src="sample-img-15.jpg" alt="#" height="24" width="100" onContextMenu="return false;" />
There is no way to protect image downloading. This is because the image has to be downloaded by the browser for it to be seen by the user. There are tricks (like the transparent background you specified) to restrict certain operations like image right click and saving to browser cache folder, but there isn't a way for truly protecting the images.
1. Disable the Right Click on all Images
let allImages = document.querySelectorAll("img");
allImages.forEach((value)=>{
value.oncontextmenu = (e)=>{
e.preventDefault();
}
})
2. Disable the Pointer Event Using CSS
img{
pointer-events: none;
}
3. Put a transparent overlay over all the Images
<div class="imageContainer">
<div class="overlayDiv"></div>
<img src="Image.jpg" alt="Image">
</div>
And some CSS like this
.imageContainer{
position: relative;
}
img{
position: absolute;
width: 100%;
height: auto;
top: 0px;
left: 0px;
}
.overlayDiv{
position: absolute;
width: 100%;
height: auto;
top: 0px;
left: 0px;
background-color: transparent;
z-index: 2;
}
4. Put your Image as a Background Image
div{
background-image: url(Image.jpg);
background-size: cover;
}
These methods will only work on normal users because they most probably don't know about the inspector or how to check source code.
But a web developer can easily download these files, there is no such way you can disable the inspector completely.
At the end i would like to add few words.
Technically, Now think about this you are sending a Image from your server to another computer over HTTP, and you are at the same time trying to prevent it, it doesn't make any sense.....
you should always assume that anything that enters the machine of the user can be retrieved back now or later, no matter how hard you try, to hide it with encryption or maybe like youtube, sending the thing in chunks, and collecting them in browser.
getting the image ultimately is hard for a common user but not for people with a lot of technical background, maybe they are intercepting the entire network on Operating System Level, how you gonna stop them there.
As some people already said that it is not possible to prevent people to download your pictures, a trick could be something like this:
$(document).ready(function()
{
$('img').bind('contextmenu', function(e){
return false;
});
});
This trick prevents from the right click on all img. Obviously people can open the source code and download the images using links in your source code.
There is no full-proof method to prevent your images being downloaded/stolen.
But, some solutions like: watermarking your images(from client side or server side), implement a background image, disable/prevent right clicks, slice images into small pieces and then present as a complete image to browser, you can also use flash to show images.
Personally, recommended methods are: Watermarking and flash. But it is a difficult and almost impossible mission to accomplish. As long as user is able to "see" that image, means they take "screenshot" to steal the image.
Here are a few ways to protect the images on your website.
1. Put a transparent layer or a low opaque mask over image
Usually source of the image is open to public on each webpage. So the real image is beneath this mask and become unreachable. Make sure that the mask image should be the same size as the original image.
<body>
<div style="background-image: url(real_background_image.jpg);">
<img src="transparent_image.gif" style="height:300px;width:250px" />
</div>
</body>
2. Break the image into small units using script
Super simple image tiles script is used to do this operation. The script will break the real image into pieces and hide the real image as watermarked. This is a very useful and effective method for protecting images but it will increase the request to server to load each image tiles.
First realise that you will never be able to completely stop an image being downloaded because if the user is viewing the image they have already downloaded it (temporarily) on their browser.
Also bear in mind the majority of users will probably not be web developers but they may still examine the source code.
I really discourage disabling right click, this can be extremely frustrating for the end user and is not safe anyway since the image can still be dragged into a new window and downloaded.
I would suggest the method used by CampSafari i.e.
img {
pointer-events: none;
}
but with an improvement:
So first lets remove the url of your image and add an id attributes to it. Like so:
<img id="cutekitten">
Next we need to add some JavaScript to actually show the image. Keep this well away from the <img> tag you are trying to protect:
document.getElementById("cutekitten").src = "http://placekitten.com/600/450";
Now we need to use the CSS:
#cutekitten {
pointer-events: none;
}
The image cannot be dragged into a new window as well downloaded via right click.
JSFiddle
Yet another method you could use is the embed tag:
<embed src="http://placekitten.com/600/450"></embed>
This will prevent the right click.
I know this question is quite old, but I have not seen this solution here before:
If you rewrite the <body> tag to.
<body oncontextmenu="return false;">
you can prevent the right click without using "real" javascript.
However, you can't prevent keyboard shortcuts with HTML. For this, you must use Javascript.
As other answers said, if you can see it you can copy/download it.
To add up to the other answers, just for your information, you can add invisible or tricky watermarks to your images:
http://www.cgrats.com/create-an-invisible-watermark-in-photoshop.html (just an example, there are more techniques, just google for invisible watermarks)
Anyway if you want to prove the ownership of your image a good way is to have a bigger resolution copy for yourself, and always publish a lower resolution / size one. Or publish it also on a "public" media like ... deviantart or flickr or something where people can't change the upload date. This way you can prove you had that image before anybody else
Try this one-
<script>
(function($){
$(document).on('contextmenu', 'img', function() {
return false;
})
})(jQuery);
</script>
This is working form me: content: url('https://myimage.png'); in the style or css class. Than you cant right click and save the image.
img.my-image-class{
content: url('https://myimage.png');
}
You can also convert the image to base64 and put it like bellow. So if yo want to download the image you need to use developer tools, than find the class, than copy the base64 which can be long if the image is big and than you need to decode this base64 and you will have the image. So its still possible to download the image. Even if it was not possible users can make screenshots of the image on the webpage and cut it in paint or use cutting tools.
img.my-image-class{
content: url(data:image/gif;base64,...img base64.....);
}
Convert IMG to Base64: https://www.base64-image.de/
Decode Base64: https://codebeautify.org/base64-to-image-converter
Reference: How to reuse base64 image repeatedly in HTML file
It's pretty much all about how much work the "thief" is willing to put into stealing the image.
You can possible deter a lot of lazy ones by just disabling the right-click menu, creating overlays, using it as background-image, ... But anyone with limited IT skills can go into the Developer Tools, under "Network", and is able to see and copy any images that have been downloaded to the browser.
These solutions also come with some downsides. Using "background-image" will possibly prevent Google from indexing your image. No context menu can prevent the user from using other options in the context menu which can be quite annoying.
The best - and basically only - solution, is to cut the image up into small pieces server-side, and put them back together with some custom javascript. For extra protection you can store some kind of "map" along with the image, with directions on how the image should go back together. This way it's not clear to the thief how all the different downloaded tiles should fit together.
Of course anyone can always take a screenshot. But I assume you are more worried about people downloading a full size and high quality image, instead of just having a low-res screenshot version.
As we know there is no proper method to avoid image theft. But we can reduce it for some extent. We can avoid those people who are not geek in computers to download the image as well as your code.
Here are some JQuery tricks we should include in our site to reduce image theft
Disable right click
Disable Ctrl+ combination (ex Ctrl+s,Ctrl+u) [Better to disable Ctrl key ]
But user can also download the web page using developer tools in Firefox. We don't have solution for this because this will be on the client side and is provided by the user's browser.
You can find the code for all the above listed on stack overflow
this code will disable Right-Click on Win or Click and hold on mac to open "contextmenu"
$("img").on("contextmenu",function(e){return false;});
It's so simple and always works fine.
and it's not depends on OS or Browser that you're using.
I used the below code in global CSS (index.css) in ReactJS. It's working correctly. You can also try. Thanks.
img{
pointer-events: none;
}

html transfer information from one page to the next

So I have been looking into this for a few weeks and have come up with nothing!
I work on the website for my families music store, and have been asked to add a "Links" page to the website. My goal would be to have the categories of our vendors (i.e. Violin, Guitar, Piano, etc.) on the left of the page and when the category is selected the links come up on the right. That part I have. The tricky part here is: When a link to a vendor (i.e. Fender, G&L, Yahmaha) is clicked instead of taking them directly to the site, I want it to take them all to the same page, but embeded on that page is the site.
I have done a lot of research on this and have come up with nothing. I could just go through and make a new page for each of the vendors, with the embedding on each individual page, but that is extremely time consuming with the amount of vendors.
Is something like this at all possible? I've been playing with embedding itself and have that down. It just comes down to, which link did they click, and loading that specific page.
If there is any more information you may need to help or point me in the right direction please let me know! Same with any code that may be helpful!
I've come up dead on all my research on this.
EDIT: I guess my ultimate goal is that it will look something like this: http://answers.yahoo.com/ so that the vendors website is open on bottom, but our stores banner and links are still at the top. Out website can be found here: http://www.brassbellmusic.com/default.aspx
I've created a JSFiddle to demo this functionality using jQuery.
We iterate through the list of brand links in the ul:
$('#brandListing a')
Capturing a click event on each:
.click(function(ev){
jQuery passes an event object to the anonymous function (ev). When the link is clicked, we first must prevent the default action, which is to follow the link. Then we update the src attribute of the iframe (the "embedded page") with the value of the href that was clicked:
ev.preventDefault();
$('#embeddedBrandWebsite').attr('src', ev.target.href);
You'll need to add the jQuery library to your page to use my code sample, but it's possible to replicate the functionality without it.
EDIT
The sample above is for a single page implementation, where links and the embed are on the same page. To achieve the requested "transfer of information," I recommend passing the target href as a GET parameter (brassbellmusic.com/brandEmbed.aspx?path=http%3A//www.gibson.com/). On the single "embed" page, you can then extract this either on the server side to set the iframe's src, or using javascript. With javascript, you might use:
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
Source
And then after your document is ready, set the iframe src using getURLParameter('path').

Stop refreshing a specific div when browsing

I'm working on my website where I have a music player. The annoying part is that when I browse to another page the player stops and starts from begining...
What I want is to have a persistent music player. So how can I make the div that contains the music player to be static when browsing to another page?
The website: demo(dot)zdringhi-art(dot)com
Thanks!
WEB is stateless.
So if you move to another page there is no way for a div to remain the same.
Although what you can do is that... Hmm as follows.
Have a single page and have your div in there.
Then the other part of the page is loaded via ajax.
also when a link is clicked only parts of pages will be loaded.
Seems too much of coding , but is the only feasible option.
For eg take facebook
Gurav shah is correct, the web is stateless so if you are changing pages you only have a few options for this.
Frames, yes before anyone shouts this is what they were designed for. You could have the music player in one HTML frame and the rest of the page in another so when you move around you are only updating the main content frame.
Or do as gurav suggests and make your whole site one page and update the content with Ajax, so the music Div does not change.
Pass the current position of the player to the next page when you click a link.
to another page
Where getseconds() returns the current position of the music player and passes it to the next page then when that page is loaded you read in the variable from the URL and start the player from there.
Using frames is one solution but since you are using JQuery on your site you should check out .load (http://api.jquery.com/load/). It allows you to load the content of another page and put it somewhere in the current page. Something like this:
$(function () {
$("a").click(function (e) {
e.preventDefault(); // don't follow the link
$("#ja-container").load($(this).attr("href") + " #ja-container");
/* Load the new page using ajax request, take the content in #ja-container from that new page and put it in the current page's #ja-container */
});
});
This is not a complete solution: when someone clicks Concerts -> Agenda you should keep Agenda visible.
Personally, instead of forced background music I'd rather like to see a page with Youtube videos of the people playing the music.
Well, yes HTTP is stateless. what you can do is create a cookie, and update it with current location/time value of the player, constantly. This way, when you go to any other page you can read time/location from cookie.
otherwise, in a cookie less approach, sending AJAXed location/time data back-forth server-client will be too much network.
If I was doing this, I would have gone cookie way.

Why do page anchors sometimes miss?

On an HTML page, a link like this:
Location on Page
...should navigate to this spot on the page:
<a name="pagelocation">
But in my experience, it sometimes misses - especially when linking from another page (like <a href="somepage.html#pagelocation">). By "misses," I mean it scrolls to the wrong spot on the page - maybe close, maybe not.
Normally, the target location ends up at the top of the screen. I know this can fail if there's not enough room below the anchor to scroll it to the top of the screen.
Why else would it fail? Does it depend on layout at all? How can I fix it?
(I'm keeping this general because I'd like a catch-all reference answer.)
Update 1
Thanks for the pointers so far about non-explicit image sizes. But what about on a page where all the elements have explicit size? (I'm dealing with one now.)
Quite often the scrolling can occur before the page has finished loading. If you have images without widths and heights, the page will jump, then load the image and re-layout itself, making the place you previously jumped to seem wrong.
Edit: Anything else that can change page layout should also be considered with suspicion... this include javascript and CSS that's not loaded in the <head> (never mind that all CSS should be loaded in the head; it isn't always).
If the page is bounced through a redirect, I believe IE will scroll the end page but Firefox won't.
JS Solution
Run this function on document ready.
function goToAnchor() {
hash = document.location.hash;
if (hash !="") {
setTimeout(function() {
if (location.hash) {
window.scrollTo(0, 0);
window.location.href = hash;
}
}, 1);
}
else {
return false;
}
}
I believe the behavior you are seeing is the result of the browser locating to that spot on the page before all images have finished loading. Once the images finish loading, then the layout of the page has changed (the page is likely longer vertically, for example), causing the location of where the anchor should be to have changed - but the browser still thinks it has already navigated to that anchor.
As mentioned above, this is probably due to images being rendered late and 'adjusting' the layout as they load.
If you can specify the size of the images then that much room can be allocated before they render, which should prevent the problem.
As a side note I've had this problem before in the form of using forward/back between enough pages that the images needed reloading, causing me to end up in the wrong place after they had rendered.
I have also seen this happen when JavaScript creates a drop-down menu at the top of a page. Then, once the menu has been finished, it is hidden, scrolling up the content below.
In the meantime, the browser has already set the target location at the top of the window. Hiding the menu a the very top of the page moves the target location up off the top of the window.
Note that you can add id="pagelocation" to just about any HTML element, for the same result, which saves you adding the additional anchors for link destinations.
OK. I think this is new. Using HTML5's autofocus will cause a misfire, as will jQuery's focus() method. Took 90 minutes of trial and error to discover this because I thought the issue was image related :)