Goodmorning,
I've this situation:
I'm working with Wordpress and I'm trying to create an affiliate box for our affiliates. I've created it dynamically and you can see one example here
Values for the creation of the box are passed with $_GET and the box is created by a php function called before the_content() on the template page.
The box, if you load the page directly, works fine.
But I'm trying to load that specific div into another page and for test I'm trying to load it into a page of my same domain.
The page for the test is here
I'm using jQuery load() function for the loading, like this:
jQuery(document).ready(function(){
var valore = jQuery('#cm_aff_box_container').text();
jQuery('#cm_aff_box_container').empty().load('http://www.clubmagellano.it/affiliate-box/?value='+valore+' #cm_affiliate_box', function(){/*Other stuff here*/});
});
});
Unfortunately it won't load anything in the div. I've tried to load the page directly and it loads correctly despite the fact that also in this case my affiliate box is not loaded.
I've also read that the load() function won't work for other domains and I need that portability. How can I solve this problem?
==========UPDATE===========
I've resolved, and the box is loaded within my domain. The problem is, how can I load it on another domain?
==========UPDATE 2.0 ===========
Guys I've resolved putting this:
<?php header("Access-Control-Allow-Origin: *"); ?>
To the header.php file of the domain which create the box. It works perfectly!
Why not use an iframe? It's typically used to display ads in a page.
<iframe id="cm_affiliate_box"></iframe>
.
.
.
jQuery(document).ready(function(){
jQuery('#cm_affiliate_box').attr('src', 'http://www.clubmagellano.it/affiliate-box/?value='+valore+' #cm_affiliate_box');
});
If everyone is having this problem follow this solution:
Put this code to the header.php of your domain template, the domain which has to be loaded.
<?php header("Access-Control-Allow-Origin: *"); ?>
The load() jQuery function will recognize as trusted the website and will load the box. It's perfectly working!
If you don't want to allow all domains but only some, instead of '*' you could write the link of domain/s allowed to retrive the box.
Related
I am hosting a quiz on my website from an iFrame. I want the quiz share to share the url of the site hosting the iFrame and not the IFrame url.
I have tried:
echo $_SERVER['URI']; and $_POST['URI']
Which return nothing. I also want to include paramaters. Here is the url: http://quizzerland.com/?q=f
From the server perspective the iFrame is a stand-alone website and is requested separately by the browser.
If you control the code on the hosting / parent website, you can make the parent website pass its URL to the iFrame with Javascript.
Just add something like this at the bottom:
<script>
document.getElementsByTagName('iframe')[0].contentWindow.parent_window_url = window.location.toString(); /* or window.location.host */
</script>
Then in the quiz's page (the iframe) javascript code you have access to the variable parent_window_url which contains the parent's URL. You can POST it via AJAX to the server or include it in URLs you want to generate dynamically.
My question is: How to get the URL of webpage where php file is loaded with ajax?
I need to get this variable within ajax file not the main page file.
Everything works perfectly when I manually add the URL of a webpage to retrieve facebook likes:
$facebooklikesURL = 'http://www.example.com/';
but I want to make it dynamic. I'm using wordpress, but it looks like when you're loading file via Ajax, it ignores any functions outside the file - like get_the_permalink(); .
I tried this method:
$facebooklikesURL ="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
... but it retrieves file location (which I'm loading with ajax) not the Webpage URL where it's loaded.
Can anyone help me with this?
This is a bit of a follow on from this question/answer: https://stackoverflow.com/a/4152528/348922
I'm simply not sure how to apply this to my situation (if it's at all possible).
I have a container div that when a button is clicked a file is loaded into the div via jquery:
var root = location.protocol + '//' + location.host;
$(".button-book").click(function(e) {
e.preventDefault();
$('#container').load(root+'/loaded-file.php');
});
Fine. BUT that file has a number of text strings that I need wrapped in php in order to hook into them for translation purposes (using WPML plugin for Wordpress):
<?php _e('Arrival Date', 'mywptheme'); ?>
<?php _e("Day", 'mywptheme'); ?>
<?php _e("Month", 'mywptheme'); ?>
<?php _e("Year", 'mywptheme'); ?>
// etc...
Obviously this doesn't work when the file is loaded dynamically. Is it at all possible or am I completely wasting my time?
Your issue is that _e(...) is a wordpress function, so when this file (loaded-file.php) is executed outside of wordress, it does not work. Its not actually anything to do with jquery - if you visit the file directly in your browser it wont work either.
Simply add the following to the top of loaded-file.php:
require($_SERVER['DOCUMENT_ROOT'].'/blog/wp-blog-header.php');
Adjust for your actual wordress location, in the above case wordpress is in domain.com/blog/
This is a fantastic resource and I have been using it extensively during my first website developing project.
I have static webpages that I want to maintain static but introduce WordPress (already installed and running on a /blog/ subdomain) onto the static homepage within a specified div. I found the code:
<script typ="text/javascript">
$(document).ready(function(){
$("#randomdiv").load("load.php");
});
</script>
Which I modified from "#randomdiv" to "#myblog" and "load.php" to the "blog/index.php" file that WordPress added. I'm also specifying that I want #content from index.php.
<script typ="text/javascript">
$(document).ready(function(){
$("#myblog").load("blog/index.php #content");
});
</script>
<div id="myblog" class="grid_8">
"Loading..."
</div>
The kicker: it worked for about an hour last night. I was styling the php in the CSS for my static page and it stopped working! I have cleared everything and started over but no luck. Where am I going wrong???
--Thank you for your responses--
Maybe someone can quickly and easily explain to me how .php works. WordPress gives me about 15 .php files inside /blog/ and I assumed index.php was my ticket, but maybe not. Still, lat night this worked. I posted a new post and it appeared, I was styling it and everything. Something has shifted and I can't find it.
WordPress was placing my index.php at http:/ / ... com/blog/index.php instead of http:/ /www ... com/blog/index.php.
I went into my WordPress dashboard, navigated to settings, and changed WordPress address (url) and site address (url) to include "www".
Try $('#myblog').load('blog/index.php'). I you only need the #content element from the HTML loaded - consider requesting an another url that will return only the part of HTML code you need.
Also, probably, you broke your HTML when styling it, so jQuery does not find the #content element when HTML it is loaded with .load function - then it does not (and should not) show any error messages.
I've searched the entire web for a clear answer but haven't found one yet.
I have this site:
www.helixagent.com/todo/
and I want to load that entire site into a portlet in my main index page.
I tried an iFrame approach but it fails to load the site correctly. The styles and javascript files don't get loaded (i think)
Is there a way to make a portal that mimics a new browser page and load the content exactly as it would if it was directly accessed via the url bar?
It’s possible to perform cross-domain requests using jQuery.
Simply include this file after loading jQuery, and then you can do funky stuff like:
$('html').load('http://helixagent.com/todo/');
Note that this of course won’t load the embedded CSS and JS files. You’ll need to use an iframe for that:
$(function() {
$('body').append('<iframe src="http://www.helixagent.com/todo/" />');
});
Live example: http://jsbin.com/uruka
Here's an example using jQuery and iframe:
$(document).ready(function(){
$('#somediv').html("<iframe src='http://www.helixagent.com/todo/'></iframe>");
});
This should be placed inside the <head> tag of your page and it will load the entire site with all the js and css too into a div.
Have you looked at using Server Side Includes.