I recently ran into a situation where I wanted to pull some content from another web site, however, due to a configuration constraint, I was unable to to this in a normal fashion.ย Essentially, there was a table of data (a subset of the content on the page) on another site which I wanted to include on my web page, but using an IFrame to display the content simply wasnโt an option (due to the fact, I only wanted a subset of the data on the page).ย In fact, I wanted to pull the data and display it in a web part, embedded in a web part page in SharePoint.ย The main issue was that since I was doing this across domains (which is a big no-no for many security reasons), I had to come up with another technique.
After searching around the web for a while and trying to come up with a decent technique, I fell upon this thread on stackoverflow.com http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy.ย ย Thereโs a number of noted techniques in there, but the one I chose was not one of the many highlighted earlier on in the thread, but instead a little one buried later on where someone discusses โanyorigin.comโ.ย Basically, ruling the items out one by one, iframe doesnโt work, as I wanted sub-content.ย The AJAX discussion theyโre talking about sounded very browser specific, and also quite flaky, depending on the browser version.ย And window.postmessage wasnโt an option as I didnโt have direct access to the content on the other end, so I had no way of embedding anything that into the destination.
How anyorigin.com works is that you provide it a URL youโre wanting to retrieve, and it will provide you back the content from that site, basically the entire web page, as a string in JavaScript (or jQuery if you want to call it that).ย You can then take that string and set it as the innerHTML value of a div (or other DOM node), and voila, youโve got the content from the other site.ย A little jQuery tweaking, and youโve got sub-content from that site.ย This was the complete solution for me โ yay!ย So how did I do it?
The Code
Disclaimer 1: Do not use this technique to take otherโs intellectual property or copyright material.ย This is only meant to be used on content you already own, or have permission to use.
Disclaimer 2: The possibility of having a cross site scripting attack is very real by using this technique โ below, Iโve added a line which strips out any inline JavaScript โscriptโ tags for this very reason โ I highly recommend you do not remove that security precaution.
So what my solution looks like is below.ย Essentially, itโs a block of code I embed into a SharePoint script editor web part (in 2013 โ in 2010 use a content editor), and it runs itself when the page loads.
Simply replace the yellow highlighted text below with the website URL youโre looking to query for data.ย And the purple text below with the location of the content (see the comment).
//Start of code
<script src=”http://code.jquery.com/jquery-1.9.1.min.js”></script>
<script type=”text/javascript”>
var ExternalURL = “www.dummysite.com“; // This address must not contain any leading “http://”
var ContentLocationInDOM = “#someNode > .childNode“; // If you’re trying to get sub-content from the page, specify the “CSS style” jQuery syntax here, otherwise set this to “null”
$(document).ready(loadContent);
function loadContent()
{
var QueryURL = “http://anyorigin.com/get?url=” + ExternalURL + “&callback=?”;
$.getJSON(QueryURL, function(data){
if (data && data != null && typeof data == “object” && data.contents && data.contents != null && typeof data.contents == “string”)
{
data = data.contents.replace(/<script[^>]*>[sS]*?</script>/gi, ”);
if (data.length > 0)
{
if (ContentLocationInDOM && ContentLocationInDOM != null && ContentLocationInDOM != “null”)
{
$(‘#queryResultContainer’).html($(ContentLocationInDOM, data));
}
else
{
$(‘#queryResultContainer’).html(data);
}
}
}
});
}
</script>
<div id=”queryResultContainer”/>
//End of code
This is a simple way to be able to pull content (or sub-content) from another web site into a web part on your SharePoint site.
Thanks for your nice tutorial. how to send setHeader to remote site using anyorigion.com? for example send user agent and sessionID ?
I’m not sure I really understand your question. Can you please be more specific?
So how exactly should I denotate the subcontent I would like to request?
You should just be able to simply set the variable ContentLocationInDOM to whatever jquery selector you’d like to use, and it will return the sub-content based on the selector you’ve identified.
Thanks, I am inexperienced with this.
I did that and tried to execute the code but I had issues with:
data = data.contents.replace(/]*>[sS]*?/gi, โ);
Specifically, the near the end of the line that my browser (chrome) takes as an end of the script.
Thanks for your help and for posting your code.
– Raul
I assume your comment stripped out the script tags, and that your code actually looks like mine in the post. That line is there to strip out all all embedded JavaScript, so that it can’t be used maliciously against you (on the rare occasion that you were pulling in malicious code). You could try without that line to see if the rest worked without that line.
This is my url:
http://truelife.vn/offica/truelifetv/action?communityId=1376442&_f=110&c=vstv036&q=high&type=tv&callback=Ext.data.JsonP.callback10
i wana get text form that url, can you help me ?
thanks in advance
Hi,
I have created a web part and made it as a wsp file and deployed in the server and added the web part in one of my list.
The images, css and script files has been added in the Style Library.
when I add the webpart in one site it was working fine, but the same web part when added in another site its not working, js files are not referenced.
I am really stuck with this issue for the past one week, please provide my some solutions.
Thanks,
Ramkumar K
Hi,
Thanks for your tutorial.
I want to pull a sub-content from my website into a facebook page tab using the script you provided.unfortunatly, it doesnt work.
Your help will be so much appreciated
Best regards,
Are you trying to do this in SharePoint? The script is meant for use in a content editor web part in SharePoint.
Hi again,
First,i just wanna thank you for yout fast support.
I understand now why it didnt work:i tought that i can use it in an html page.
Are there any alternatives to this script for an html page?
Have a nice day.
PS:Sorry for disturbing you specially because my request is not related the SharePoint.
Its not working for me in a content editor webpart inside VMware i am trying with sharepoint 2010
Can you describe what you’re trying to use it for? Ie. what URL are you trying to grab content from?
Hi, great tutorial. I trying to do something similar. I have a mobile web app and I have content ( HTML) page that is on my server I’d like pass it into a div. I seen .load() or .get I just don’t now how to the correct syntax should. be to. Any thoughts how I can do this
This is great! Thank you so much.
PS! ๐ Your code: . You need to close the div tag.
It’s a self closing element (<Div/>) XHTML 4 compliant.
Hi,
Thanks for your tutorial,
I am trying to do something similar but load the content from a virtual directlory on the same server.
I am using $().load or $.get(), both works for loading the html content into a div in my page. But the images and other js/css references are not loaded as the parent site is searching for content in its root folder, where as the images are in the Virtual directory folder. ANy ideas on how load images from Virtual directory ? Any suggestion would be helpful
Thanks!
you background make it hard to read. change darn it
I like your code, but I would like to be able to use it in an HTML setting on an internal webpage.
I would like to pull data from our “public” site and display it in house. How can I only display the i want to?
This is meant to be used in a SharePoint content editor web part. Usage outside of SharePoint is not supported.
http://truelife.vn/offica/truelifetv/action?communityId=1376442&_f=110&c=vstv036&q=high&type=tv&callback=Ext.data.JsonP.callback10
Can Anyone help me ?
I wana get string text from this url.
Can I use this code to specifically pull a line item from the source code of another site?
For instance
Line 163=( 1845.62)
and I would like to create an iframe that would pull that 1845.62 value into my website on a daily basis.
Thanks for your help.
This is mainly meant to put into a content editor webpart within SharePoint. If that’s your intention, then sure, that should work.
So how would I put this into the code above? That value in line 163 updates every 15 minutes and I would want anyone getting on our website to see the latest value from that string.
Thanks so much for your help.
Are you doing this in SharePoint? If so, the article describes exactly how to set it up for a page in SharePoint. If you want to use outside of the world of SharePoint, that should be fine, it would just need to be somewhat adapted to working outside of the SharePoint framework.
I don’t have Sharepoint installed on my wordpress website. Not saying that I couldn’t install it if necessary but am mainly trying to accomplish this within a wordpress widget that shows that line code value whenever someone gets on the site.
Trying to pull from one site collection to another in SP Online 2013 (O365) and this does absolutely nothing. Also tried ot with the url of this site – nothing.
I want to extract certain component say a jquery slider along with its css and all the jqueries used in it. Is the method u mentioned works in my case?
Unfortunately, that is really not what this is meant for.
In the jsp page of my web application I need to take the contents of another webpage can I use the above code for the same ?
Thanks in advance ๐
The webpage url is something like http://one.abc.com/Pages/home.aspx
I tried the above code and replaced my url but couldn’t read out the content
Please help me out
This solution is meant for SharePoint. Are you using SharePoint?
No I am not using sharepoint . I have a web application where I need to get the contents of another site
Sorry, this is meant as a solution for SharePoint. Outside of that, I would look elsewhere for a solution. I would start with anyorigin.com.
Thanks so much for this. I utilized it in Tumblr, not Share Point, but it worked perfectly after removing the line you added in Disclaimer 2. As is, it kept outputting the code as plain text after the closing script tag.
Also, if anyone else is having issues, I had to replace all the quotes from this code block with straight quotes.
Can anyone please tell me is there a way to get current url of iframe cross domain???
Does anyone have a working example?
I want to show images from tags in WP page.
Images are hosted on a subdomain in Media gallery and i want to show various tags in page and post.
how can various tags be displayed together.
regards
You’re code didn’t work for me.
I’m not sure how AnyOrigin can function if SharePoint resides on-prem?
Good Morning! I have been looking all over the web for a similar issue. But in my circumstance, I need to be able to update the SharePoint list with an external xml file I have access to. Any assistance in this matter would be greatly appreciated, thank you!
would you be able to post a screen shot of what the output lookkkslike? thks
If I want to pull content from nytimes.com from soccer section what would be the code?
Does this script work for SharePoint 2016?
Yes, it should. It’s client side code, so it should work in every version of SharePoint.