I am writing a page that grabs images from a url the user inputs, and using ajax, am updating a div with the img src to be displayed by fancybox in a modal window. The images that are displayed have titles displayed below, as links. This is done with fancybox (If you set the title as set the title="something" will display "something" below the image. If you set the title as title="<a href='something.html'>Something</a>)
My problem is this. I need to allow the user to choose an image by clicking the link, and in turn have it do 2 things: update the hidden field with the src url of the img, and submit the form.
Here is the code I have for the ajax request:
function getCustomerInfo() {
var phone = document.getElementById("urls").value;
var url = "grabimages.php?urls=" + escape(phone);
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
$("#dvloader").show();
$("#selectimages").hide();
//$("#noimages").hide();
function updatePage() {
if (request.readyState == 4)
if (request.status == 200){
if(request.responseText == ''){
$("#dvloader").hide();
$("#noimages").show();
}else{
document.getElementById("update-div").innerHTML = request.responseText;
$("#dvloader").hide();
$("#selectimages").show();
$(".fancybox")
.attr('rel', 'gallery')
.fancybox({
padding : 15
});
$('input[id=theimage]').click(function(){
$(".fancybox").eq(0).trigger('click');
});
function recURL(recipeurl) {
$('#recipeurl').val('recipeurl');
document.recipeform.submit();
}
}
}else{
alert('nothing');
}
}
And here is the code I tried for the href javascript call (this is the server side code, so PHP:
echo "<a class=\"fancybox\" id=\"recipelinks\" title=\"<img src=\"" . $imagepath . "\" alt=\"\"/>";
This doesn't do anything. Any suggestions?
Use $('.fancybody').live('click', function() { });, live allows you to listen for events on elements which are added to the DOM dynamically.
Related
I have a php page with a nav and a content div. When I choose an option in the nav, the content is modified by loading another php file inside it. For this I use ajax with load function. My problem is that if I right click and select open in a new tab or a new window, evidently, only the content is open in the tab.
I know that I can have the whole page (container + content) in each php file and load only the content div, but I think there is no much sense in this.
Is there any way to get the container along the content in the new tab?
I already had a similar situation, the tabs have pages, and the menu was in the top page. you can apply this solution:
This logic will work with iframe, if you are using divs, you can adjust for this:
main page with the nav menu, here you need apply some logic like this:
<script>
//Object to get values of URL (GET)
var request = {
get get() {
var vars = {};
if (window.location.search.length !== 0)
window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
key = decodeURIComponent(key);
if (typeof vars[key] === "undefined") {
vars[key] = decodeURIComponent(value);
}
else {
vars[key] = [].concat(vars[key], decodeURIComponent(value));
}
});
return vars;
},
getParam: function (param) {
var vars = request.get;
if (vars[param] != undefined) {
return vars[param];
} else {
return null;
}
}, getParams: function () {
return request.get;
}
};
//variable that defines if the client are in mainWindow
window.mainWindowFrame = true;
var tabName = request.getParam("tab");
console.log("tabName",tabName);
//call some function to reload the content of iframe or div to requested tab.
//if(tabName != null || tabName != ""){
//tabs.load(tabName) ....
//}
</script>
In the pages of content you need aplly this piece of code to reload the page if the menu are not present:
<script>
//name of this tab
var tabName = "someTab";
//check if the menu are present here
if(window.top.mainWindowFrame == undefined){
window.location.href = "mainpage.html?tab="+tabName;
}
</script>
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?
In my website authors (users) can mark posts as favorite.
It works this way:
if ($favinfo == NULL || $favinfo == "") {
$favicon = "ADD"; .
}
else {
$favicon = "REMOVE";
}
Its suposed to look dynamic, it works, when user click ADD, it adds the post to his favorites and reload the page with the REMOVE link.
The problem is its not really dynamic it reloads all the page.
How can i only reload that link (wich is inside a div)?
I know i have to use ajax, jquery, etc, but i tried some examples found here in S.O. but no success.
$('a').on('click', function(e){
// the default for a link is to post a page..
// So you can stop the propagation
e.stopPropagation();
});
Including this stop you page from reloading your entire page
If you want it to be dynamic, you will need to use AJAX. jQuery has ajax support which makes this really easy. If you are not familiar with ajax or javascript you should read up on it first.
PHP
if ($favinfo == NULL || $favinfo == "") {
$favicon = "<a class=\"fav-btn\" data-id=\"".$articleinfo['id']."\" data-action=\"add\" href=\"".$siteurl."/author/favorites.php"\">ADD</a>"; .
}
else {
$favicon = "<a class=\"fav-btn\" data-id=\"".$articleinfo['id']."\" data-action=\"remove\" href=\"".$siteurl."/author/favorites.php"\">REMOVE</a>";
}
JavaScript
$('a.fav-btn').on('click', function(e){
var $this = $(this), // equates to the clicked $('a.fav-btn')
url = $this.attr('href'), // get the url to submit via ajax
id = $this.attr('data-id'), // id of post
action = $this.attr('data-action'); // action to take on server
$.ajax({
url: url+'?'+action+'='+id
}).done(function(){ // once favorites.php?[action]= is done...
// because this is in .done(), the button will update once the server has finished
// if you want the link to change instantly and not wait for server, move this outside of the done function
if(action === 'add'){
$this.attr('data-action', 'remove').html('REMOVE'); // update the button/link
}else{
$this.attr('data-action', 'add').html('ADD');
}
})
return false; // prevent link from working so the page doesn't reload
}
If you are okay with using JQuery, you have some tools to accomplish this.
Have a structure / method of identifying your links.
You can have a click() listener on your add button that will call a JQuery $.post(url, callback) function.
In that callback function, you can have it update the corresponding DIV (that you defined in #1) with a 'remove' link. i.e if you identify the DIV by ID, you can retrieve it via $('#id') and then update that object.
The same idea can apply with the 'remove' link that you add.
So, generally...
<button id="add">Add</button>
<div id="links"> ...</div>
<script>
$('#add').click(function() {
$.post('your url',
function(data) {
var links = $('#links');
// update your links with 'remove' button, etc
}
);
});
</script>
I am making a picture preview with jCarousel (http://sorgalla.com/projects/jcarousel/), but faced with one problem: I do not know how could I reload/refresh jCarousel dynamic list.
I have few categories of images. When I click on a picture in one of that, I need that the list would be created and preview start with that element. I have some code, but do not know how to make it re-create all list after clicking on other image that preview start with other image.
Here are my code:
$(document).ready(function(){
$("ul#stage li").live('click', function() {
var ul = $(this).parent();
var index = +(ul.children().index(this))+1;
var mycarousel_itemList = [ ];
$('li[data-id]').each(function () {
var $this = $(this);
mycarousel_itemList.push({
url : $this.attr("data-img"),
title : $this.attr("data-title")
});
});
function mycarousel_itemLoadCallback(carousel, state) {
for (var i = carousel.first; i <= carousel.last; i++) {
if (carousel.has(i)) {
continue;
}
if (i > mycarousel_itemList.length) {
break;
}
carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[i-1]));
}
};
function mycarousel_getItemHTML(item) {
return '<img src="' + item.url + '" width="800" height="600" alt="' + item.url + '" />';
};
alert(index);
jQuery('#mycarousel').jcarousel({
itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback},
size: mycarousel_itemList.length,
scroll: 1,
start: index,
wrap: 'last',
animation: 'fast',
visible: 1
});
document.getElementById('popup-content').style.background='url('+$(this).attr("data-img")+') no-repeat center';
document.getElementById('fades').style.display='block';
document.getElementById("light").style.display = "block";
$("#light").fadeTo("slow", 1);
});
});
Everything is like that: there are images > I click on one of those > popup shows with jCarousel and one visible image and then I could scroll through all other images.
It is working good, but just a first time. When I click on other image (after closing popup), the view starts with that image which was opened last.
If something are not clear enough - please, ask. I will try to make it more precisely.
Thanks for your help!
You can recreate jCarousel, first use $('#mycarousel').remove(); and next init jCarousel again. I didn't really understand what you're trying to do, but this can help in most cases, of course jCarousel should have Destroy method but it hasn't.
And why you don't use jquery selectors in some cases?
Hey guys. I'm usign a js/ajax script that doesnt work with internet explorer. Firefox its ok.
Btw the head tag, im using this:
$(document).ready(function () {
//Check if url hash value exists (for bookmark)
$.history.init(pageload);
//highlight the selected link
$('a[href=' + document.location.hash + ']').addClass('selected');
//Seearch for link with REL set to ajax
$('a[rel=ajax]').click(function () {
//grab the full url
var hash = this.href;
//remove the # value
hash = hash.replace(/^.*#/, '');
//for back button
$.history.load(hash);
//clear the selected class and add the class class to the selected link
$('a[rel=ajax]').removeClass('selected');
$(this).addClass('selected');
//hide the content and show the progress bar
$('#content').hide();
$('#loading').show();
//run the ajax
getPage();
//cancel the anchor tag behaviour
return false;
});
});
function pageload(hash) {
//if hash value exists, run the ajax
if (hash) getPage();
}
function getPage() {
//generate the parameter for the php script
var data = 'page=' + encodeURIComponent(document.location.hash);
$.ajax({
url: "http://pathfofolder/js/loader.php",
type: "GET",
data: data,
cache: false,
success: function (html) {
//hide the progress bar
$('#loading').hide();
//add the content retrieved from ajax and put it in the #content div
$('#content').html(html);
//display the body with fadeIn transition
$('#content').fadeIn('slow');
}
});
}
The loader.php contain the php code to get pages, something like:
switch($_GET['page']) {
case '#link1' : $page = 'contenthere'; break;
}
echo $page;
So, on the links, i'm using Link 1 to load the content into the div content.
The script does works well with firefox, but with internet explorer it doesnt load the content. Could someone pls help me to fix this?
It not go into the success function at all on IE, and i'm getting no html error from IE too.
Best Regards.
Make sure your html is sounds. FF tends to auto fix the syntax.