0

I have a system of posts, where the publisher can embed code another site to reference content. However, I am having a problem with the LookBook.nu site.

The site of this embedded code contains a script tag that calls jQuery 1.6 and causes my system to crash.

I wanted to remove this script tag with regex. Is this possible?

This is the embedded code:

<!--BEGIN HYPE WIDGET-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script src="http://lookbook.nu/look/widget/7625244.js?include=all&size=medium&style=button&align=center"></script><div id="hype_container_7625244">
</div>
<!--END HYPE WIDGET--> 

Ps: impractical use noConflict is because I have to replace all the scripts of my system. It would take me too long to do this.

** Sorry, I don't speak Engish. Google Translate is my best friend. :D

9
  • 3
    When I see these kind of questions I always think of this answer stackoverflow.com/questions/1732348/… Commented Jul 28, 2015 at 12:28
  • 1
    It's possible, but pointless. You can't remove it from the DOM with client side JS until it has been parsed. But the time it is parsed, the code has already executed. Commented Jul 28, 2015 at 12:30
  • 1
    Stick them in an iframe or ask them to not include it. Commented Jul 28, 2015 at 12:33
  • @charlietfl impractical to use noConflict because... Commented Jul 28, 2015 at 12:38
  • 1
    @Grimbode - Classic!! By far my favorite SO answer of all time! Commented Jul 28, 2015 at 12:43

1 Answer 1

1

The big question in this case is how the snippets get into your HTML source. If you load the code dynamically via AJAX and add it with .innerHTML() (or JQuery's .html() method for that matter), you can first filter it with a regex and replace the offending script tag. I'd try somthing like this:

\<script\s+src\=["'][^>]*?jquery[^>]*?\>\<\/script\>\n?

If you use this and replace it with an empty string BEFORE you inject the code into your DOM, than you should be fine.

If you already output the code serverside, e.g. in PHP you could also replace the tag with preg_replace() before echoing it.

The important thing is only to do the clean up before inserting it into the DOM.

P.S.: pattern updated to prevent it from matching another tag that goes before it.

Sign up to request clarification or add additional context in comments.

2 Comments

It works, but if the first script. If another script tag before this removes both.
Indeed, but can be easily corrected. Use this pattern instead: \<script\s+src\=["'][^>]*?jquery[^>]*?\>\<\/script\>\n?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.