Right now I have this list:
<ul id="list">
<li id="active"><img src="main home page/purhome.png"></li>
<li id="active"><img src="main home page/purinfo.png"></li>
<li id="active"><img src="main home page/purgyms.png"></li>
<li id="active"><img src="main home page/purcontact.png"></li>
</ul>
And this JQuery:
$(document).ready(function(){
$("#home-link").click(function(){
$("#main_info2").hide();
$("#main_info").show();
});
$("#info-link").click(function(){
$("#main_info").hide();
$("#main_info2").show();
});
$("#gyms-link").click(function(){
$("#main_info").hide();
$("#main_info2").show();
});
$("#contact-link").click(function(){
$("#main_info").hide();
$("#main_info2").show();
});
});
When I click on the home link, the URL shows indexp.php#home, which is what i want it to show. This happens for all the links. However if i refresh the page after i click on the info link for example, the main_info div is shown, but the browser still says index.php#info. I want to read the hash somehow to show the correct div depending on what the url says.
So for example if I click on the info link, the url shows index.php#info. When I refresh it says the same thing but the main_info div shows when the main_info2 div is supposed to be showing.
function getHash() {
var hash = window.location.hash;
return hash.substring(1); // remove #
}
So I have that code. Would I do something like this?
if hash = 'info' // if url says index.php#info
show main_info2
hide main_info
if hash = nothing // So if url says index.php
show main_info
hide main_info2
Any ideas?
EDIT**
I'm trying to do something like this to make it work:
var currentValue = window.location.hash.substr(1)
$(document).ready(function() {
$("home-link").click(function(){
if(currentValue == "home") {
$("#main_info2").hide();
$("#main_info").show();
}
});
$("info-link").click(function(){
if(currentValue == "info") {
$("#main_info").hide();
$("#main_info2").show();
}
});
});
Although that is not working, can someone check the code?
You can use this jQuery plugin to listen to hash changes:
https://github.com/cowboy/jquery-hashchange
I would recommend using a plugin if you want your code to work cross browser because this hasn't been standardized yet. Especially older browsers (IE6) require extensive hacks.
After some discussion about the different answers and problem, I came up with this solution:
$(document).ready(function(){
$(window).hashchange(function(){
var hash = location.hash;
if(window.location.hash.substr(1) == "home") {
$("#info-container").hide();
$("#gyms-container").hide();
$("#contact-container").hide();
$("#home-container").show();
}
else if(window.location.hash.substr(1) == "info") {
$("#home-container").hide();
$("#gyms-container").hide();
$("#contact-container").hide();
$("#info-container").show();
}
else if(window.location.hash.substr(1) == "gyms") {
$("#home-container").hide();
$("#info-container").hide();
$("#contact-container").hide();
$("#gyms-container").show();
}
else if(window.location.hash.substr(1) == "contact") {
$("#home-container").hide();
$("#info-container").hide();
$("#gyms-container").hide();
$("#contact-container").show();
}
$('#list a').each(function(){
var that = $(this);
that[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'selected' );
});
})
$(window).hashchange();
});
This work beautifully.
This uses the hashchange plugin mentioned above so make sure you insert the script into your head before using this function.
Here is bit of code to help you get started if you use https://github.com/cowboy/jquery-bbq:
$(document).ready(function(){
$("#home-link").click(function(){
$("#main_info2").hide();
$("#main_info").show();
});
$("#info-link").click(function(){
$("#main_info").hide();
$("#main_info2").show();
});
$("#gyms-link").click(function(){
$("#main_info").hide();
$("#main_info2").show();
});
$("#contact-link").click(function(){
$("#main_info").hide();
$("#main_info2").show();
});
$(window).bind( 'hashchange', function(e) {
url = $.deparam.fragment();
url && $( 'a[href="#' + url + '"]' ).trigger('click');
});
$(window).trigger('hashchange');
});
Related
I want to open share pop up window of facebook when user clicks on a particular link (Not the share button). I want to do this in php and want to share an image.
Note: I want to provide the source of the image which comes dynamically.
Can any one help me please?
Please ask for more information if you want.
Thank you.
Update:
Below is my code:
The main page where the link is:
<li id="FB_post" style="display:none;"><a id="post_image_href" href="https://www.facebook.com/sharer/sharer.php" target="_blank">Facebook</a></li>
In the js (Click event)
$("#post_image_href").click(function(e) {
if($('.prodct_personalize_but a').html() != 'Cancel' && $('#zoom1').length>0 ){
$(this).attr('href','https://www.facebook.com/sharer/sharer.php?u='+$('#zoom1').attr('href'));
}
else if ($(".image-main-container").css('display') != 'none')
{
if ($('.product_box23').css('display') != 'none')
{
e.preventDefault();
setProperDynamicTextPosition();
var data = $('#personalise_form').serialize();
$('#loader_img').show();
$.post(ajaxurl, data + '&action=save_personalise_image', function(response) {
if (response != '' && response != undefined) {
var Aryresponse = response.split('###');
$("#post_image_href").attr('href','https://www.facebook.com/sharer/sharer.php?u=' + Aryresponse[0]);
document.location.href = $("#post_image_href").attr('href');
$('#loader_img').hide();
}
});
}
}
});
Link which is created when I click on the Facebook link and go to facebook page:
In share dilog box: the current url of the browser and in the address bar: the updated link.
For Ex. https://www.facebook.com/sharer/sharer.php?u=https://www.picpixa.com/wp-content/plugins/create-own-object/test-img/final_ProductImage_1405751994-2197.png
Please let me know if you want more information.
Update In JS Function:
I have made change in my js (click event function):
Below is the updated function:
$("#post_image_href").click(function(e) {
var url='';
if($('.prodct_personalize_but a').html() != 'Cancel' && $('#zoom1').length>0 ){
//$(this).attr('href','https://www.facebook.com/sharer/sharer.php?u='+$('#zoom1').attr('href'));
url = 'https://www.facebook.com/sharer/sharer.php?u='+$('#zoom1').attr('href');
console.log("If: "+ $('#zoom1').attr('href'));
console.log("URL: "+ url);
}
else if ($(".image-main-container").css('display') != 'none')
{
if ($('.product_box23').css('display') != 'none')
{
e.preventDefault();
setProperDynamicTextPosition();
var data = $('#personalise_form').serialize();
$('#loader_img').show();
$.post(ajaxurl, data + '&action=save_personalise_image', function(response) {
if (response != '' && response != undefined) {
var Aryresponse = response.split('###');
//$("#post_image_href").attr('href','https://www.facebook.com/sharer/sharer.php?u=' + Aryresponse[0]);
url = 'https://www.facebook.com/sharer/sharer.php?u=' + Aryresponse[0];
console.log("Else: "+ Aryresponse[0]);
console.log("URL: "+ url);
window.open(url, "popupWindow", "width=450,height=400,scrollbars=no"); //If I don't use this the pop up don't open.
//In fact this is og no use as this is not opening but still I have to put this here to run below winodw.open.
$('#loader_img').hide();
}
});
}
}
console.log("The URL is: "+url);
window.open(url, "popupWindow", "width=450,height=400,scrollbars=no");
});
Another Update: Imp. Info:
I thought that this might me the issue of the Meta Tags. And it is. I am giving all the required meta tags in the main page but when I did the debug of the url I am passing it is giving me the below result.
Click here to see the result
Can anyone find me a solution?
When you click on a folder a button/link is prepended to the nav section but when i click on the added button it will not work. It does not recognize my click event for some reason..?
quick video of what i am dealing with. https://www.youtube.com/watch?v=NsW1uLbRd9w
I am clueless on why it is not working ahhhh
Any help would be awesome, thanks.
If you need more code i can send it if needed.
<script>
$(function () {
// When folder is clicked open it
$('button.<?php echo $row->folderName; ?>').bind('click', function() {
$('.open.<?php echo $row->folderName;?>').show();
// if folder is open then Prepend homebtn button to nav
var k = $('.open').css('display');
if(k == 'block') {
$('.nav').prepend('<a class="gohome">HELLO</a>');
}
});
$('.gohome').live('click', function() {
$('.open.<?php echo $row->folderName; ?>').hide();
});
});
</script>
To bind to future elements you need to bind the event to a parent that exists at the time that the procedure is executed. As your link as being added to .nav, that's what we can use. I am assuming .nav. exists when this code is run. If not, use $(document).on('click'...
$('.nav').on('click', '.gohome', function() {
$('.open.<?php echo $row->folderName; ?>').hide();
});
on http://zentili.koding.com i've got this javascript that loads the content of the linked menu item inside the main #content div of the index page, and applies an hash with the name of the loaded page minus the '.php', otherwise it loads the hash + '.php' if it's entered in the url. works very good. On other hand, the ENG/ITA entries add ?locale=lang_LANG inside the url, right before the hash, so that localization is also working fine. If you look well, you may notice that when you switch between ENG and ITA, the index-content appears just for one moment before going to the hash. I know this is because the page is first loaded, then taken to the hash but i was wondering if there some way for hiding the homepage and going directly to the hash location when it's loaded.
Here the code for my menu:
<script type="text/javascript">
$(document).ready(function() {
var hash = window.location.hash.substr(1);
var href = $('#menubar a.item').each(function(){
var href = $(this).attr('href');
if(hash==href.substr(0,href.length-4)){
var toLoad = hash+'.php';
$('#content').load(toLoad);
$("#menubar a.item").removeClass("current");
$(this).addClass("current");
}
});
$('#menubar a.item').click(function(){
window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4);
var toLoad = $(this).attr('href');
$('#content').fadeOut('fast',loadContent);
function loadContent() {
$('#content').load(toLoad,'',showNewContent) }
function showNewContent() {
$('#content').fadeIn('fast'); }
$("#menubar a.item").removeClass("current");
$(this).addClass("current");
return false;
});
});
function goENG(){
var hash = window.location.hash;
var eng = '?locale=en_EN';
window.location.replace(eng+hash) ;
};
function goITA(){
var hash = window.location.hash;
var ita = '?locale=it_IT';
window.location.replace(ita+hash) ;
};
</script>
the functions goENG() and goITA() are called via onclick on the ENG and ITA a's. I hope to find some solution into this.
The page cannot directly go to the link. It will load in its natural order and then it will go to the hash. For what you want to achieve, there is a simple solution i believe.
Hide the main content div until the document loads. use css rule "visibility:hidden" for this
If there is any hash, load it and then make the content visible.
If there is no hash in url, make the content visible on dom load.
$(document).ready(function() {
var hash = window.location.hash.substr(1);
if ($('#menubar a.item').length > 0) {
var href = $('#menubar a.item').each(function(){
var href = $(this).attr('href');
if(hash==href.substr(0,href.length-4)){
var toLoad = hash+'.php';
$('#content').load(toLoad, function(){
$('#content').attr('visibility', 'visible');
});
$("#menubar a.item").removeClass("current");
$(this).addClass("current");
} else {
$('#content').attr('visibility', 'visible');
}
});
} else {
$('#content').attr('visibility', 'visible');
}
});
--UPDATE--
If you are setting #content as "visibility:hidden"
$('#content').attr('visibility', 'visible');
should always fire, else your #content div will be invisible. The trick here is to set it to visible after we are done with checking for hash. You can keep loading the content in the div and make it visible. Making the #content div visible need not be done after entirely loading the hash.
I am submitting some data to my database then reloading the same page as the user was just on, I was wondering if there is a way to remember the scroll position the user was just on?
I realized that I had missed the important part of submitting, so, I decided to tweak the code to store the cookie on click event instead of the original way of storing it while scrolling.
Here's a jquery way of doing it:
jsfiddle ( Just add /show at the end of the url if you want to view it outside the frames )
Very importantly, you'll need the jquery cookie plugin.
jQuery:
// When document is ready...
$(document).ready(function() {
// If cookie is set, scroll to the position saved in the cookie.
if ( $.cookie("scroll") !== null ) {
$(document).scrollTop( $.cookie("scroll") );
}
// When a button is clicked...
$('#submit').on("click", function() {
// Set a cookie that holds the scroll position.
$.cookie("scroll", $(document).scrollTop() );
});
});
Here's still the code from the original answer:
jsfiddle
jQuery:
// When document is ready...
$(document).ready(function() {
// If cookie is set, scroll to the position saved in the cookie.
if ( $.cookie("scroll") !== null ) {
$(document).scrollTop( $.cookie("scroll") );
}
// When scrolling happens....
$(window).on("scroll", function() {
// Set a cookie that holds the scroll position.
$.cookie("scroll", $(document).scrollTop() );
});
});
#Cody's answer reminded me of something important.
I only made it to check and scroll to the position vertically.
(1) Solution 1:
First, get the scroll position by JavaScript when clicking the submit button.
Second, include this scroll position value in the data submitted to PHP page.
Third, PHP code should write back this value into generated HTML as a JS variable:
<script>
var Scroll_Pos = <?php echo $Scroll_Pos; ?>;
</script>
Fourth, use JS to scroll to position specified by the JS variable 'Scroll_Pos'
(2) Solution 2:
Save the position in cookie, then use JS to scroll to the saved position when page reloaded.
Store the position in an hidden field.
<form id="myform">
<!--Bunch of inputs-->
</form>
than with jQuery store the scrollTop and scrollLeft
$("form#myform").submit(function(){
$(this).append("<input type='hidden' name='scrollTop' value='"+$(document).scrollTop()+"'>");
$(this).append("<input type='hidden' name='scrollLeft' value='"+$(document).scrollLeft()+"'>");
});
Than on next reload do a redirect or print them with PHP
$(document).ready(function(){
<?php
if(isset($_REQUEST["scrollTop"]) && isset($_REQUEST["scrollLeft"]))
echo "window.scrollTo(".$_REQUEST["scrollLeft"].",".$_REQUEST["scrollTop"].")";
?>
});
Well, if you use _targets in your code you can save that.
Or, you can do an ajax request to get the window.height.
document.body.offsetHeight;
Then drop them back, give the variable to javascript and move the page for them.
To Remember Scroll all pages Use this code
$(document).ready(function (e) {
let UrlsObj = localStorage.getItem('rememberScroll');
let ParseUrlsObj = JSON.parse(UrlsObj);
let windowUrl = window.location.href;
if (ParseUrlsObj == null) {
return false;
}
ParseUrlsObj.forEach(function (el) {
if (el.url === windowUrl) {
let getPos = el.scroll;
$(window).scrollTop(getPos);
}
});
});
function RememberScrollPage(scrollPos) {
let UrlsObj = localStorage.getItem('rememberScroll');
let urlsArr = JSON.parse(UrlsObj);
if (urlsArr == null) {
urlsArr = [];
}
if (urlsArr.length == 0) {
urlsArr = [];
}
let urlWindow = window.location.href;
let urlScroll = scrollPos;
let urlObj = {url: urlWindow, scroll: scrollPos};
let matchedUrl = false;
let matchedIndex = 0;
if (urlsArr.length != 0) {
urlsArr.forEach(function (el, index) {
if (el.url === urlWindow) {
matchedUrl = true;
matchedIndex = index;
}
});
if (matchedUrl === true) {
urlsArr[matchedIndex].scroll = urlScroll;
} else {
urlsArr.push(urlObj);
}
} else {
urlsArr.push(urlObj);
}
localStorage.setItem('rememberScroll', JSON.stringify(urlsArr));
}
$(window).scroll(function (event) {
let topScroll = $(window).scrollTop();
console.log('Scrolling', topScroll);
RememberScrollPage(topScroll);
});
I had major problems with cookie javascript libraries, most cookie libraries could not load fast enough before i needed to scroll in the onload event. so I went for the modern html5 browser way of handling this. it stores the last scroll position in the client web browser itself, and then on reload of the page reads the setting from the browser back to the last scroll position.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
if (localStorage.getItem("my_app_name_here-quote-scroll") != null) {
$(window).scrollTop(localStorage.getItem("my_app_name_here-quote-scroll"));
}
$(window).on("scroll", function() {
localStorage.setItem("my_app_name_here-quote-scroll", $(window).scrollTop());
});
});
</script>
I tackle this via using window.pageYOffset . I saved value using event listener or you can directly call window.pageYOffset. In my case I required listener so it is something like this:
window.addEventListener('scroll', function() {
document.getElementById('showScroll').innerHTML = window.pageYOffset + 'px';
})
And I save latest scroll position in localstorage. So when next time user comes I just check if any scroll value available via localstorage if yes then scroll via window.scrollTo(0,myScrollPos)
sessionStorage.setItem("VScroll", $(document).scrollTop());
var scroll_y = sessionStorage.getItem("VScroll");
setTimeout(function() {
$(document).scrollTop(scroll_y);
}, 300);
How to install fancybox to show up only the first time when you com to a home page.
This shall be included:
<h1> Please select your language </h1>
<div> class="select-language" </div>
The fancybox shall show up only once, regardless which page you land on.
Try the following code with cookie,
$(document).ready(function() {
var visited = $.cookie('visited');
if (visited == 'yes') {
return false;
} else {
$.fancybox({
'width': '60%',
'height': '80%',
'autoScale': true
});
}
$.cookie('visited', 'yes', { expires: 7 });
});
You can try this.
$(document).ready(function{
if (! $.cookie("cookieName")){
// do your stuff
// set cookie now
$.cookie("cookieName", "firstSet", {"expires" : 7})
}
})
The above uses the following plugin. http://www.electrictoolbox.com/jquery-cookies/