1. Code
  2. Web Development

Quick Tip: Cross Domain AJAX Request with YQL and jQuery


For security reasons, we cannot make cross-domain AJAX requests with jQuery. For example, I can't call the load() method, and pass in ''. As we'd be loading in scripts and such, as well as our desired content, this would present a significant security risk. Nonetheless, there may be times when this is specifically what you require. Thanks to YQL, we can allow for this functionality rather easily!

The Script

Call the Function

Stripping Out the Script Tags

I had to progress rather quickly in the video, so perhaps the regular expression that strips out the <script> tags require further detail.

When we load our desired page, it's also going to load scripts! You must be very careful when making cross domain request. It definitely helps to strip out the <script> tags, but you should do more in an actual project.

Let's take the regular expression step by step.

Find all open script tags; however, they could come in many forms: <script type="text/javascript" src="bla.js"></script> , or <script type="text/javascript">lots of code here...</script> . For this reason, we add a character class ( [^>]* ), which mean, "Find zero or more of anything that IS NOT a closing bracket. This will take care of the attributes and values.

Next, we want to strip out all code, as well as any spacing. \s refers to a space. \S refers to anything that IS NOT a space. Once again, we add a * after the character class to designate that we want zero or more occurrences.

Finally, find the closing script tags.

Further Reading

This is only meant to provide a glimpse of how we can achieve this functionality. Only so much can be covered in a five minute video. Feel free to discuss in the comments, and you're always encouraged to fork the source code to improve upon it!

Looking for something to help kick start your next project?

Envato Market has a range of items for sale to help get you started.