2

Is it possible to do the following?

Say I have a Twitter script tag like so

<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>

But I want to use jQuery to remove it using .remove(); how on earth would I be able to select it as I'd essentially like to be able to pick up 'platform.twitter.com' from within the code.

This is for a little side project so if anyone successfully helps you will be mentioned on the website when its live.

2
  • I'm assuming you want to remove it before it runs. I don't think that's possible (client-side, at least) -- it won't be in the DOM for you to remove it until the browser gets to that point in rendering the page, at which point it will be run. Commented Apr 17, 2012 at 22:14
  • You should understand that removing the script after it has executed has no effect. Removing the button it injects will make it seem like it's never run, but it has. This difference is significant, because it means that changes to the injected markup (i.e. the class "twitter-share-button" changes) will break your "fix". Commented Apr 24, 2012 at 21:04

4 Answers 4

1
$('script[src$=platform.twitter.com/widgets.js]').remove();

perhaps?

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

3 Comments

Nope. The tag is just <script>. I think he wants to remove the loader script tag before it runs, not the script that is loaded by it.
@PatrickFisher cheers, now I get it! But it seems weird, since: if that script is in DOM, it's executed immediately and such the appended <script> will always be injected. He'd have to modify the surrounding source which we don't know anything of.. yet ;)
Exactly. See my comment above, which says the same thing.
1

I would try this:

jQuery("script:contains(platform.twitter.com)").remove()

Comments

0
$('script[src$=platform.twitter.com/widgets.js]').empty();

Maybe?

Comments

0

Turns out that piece of script actually writes into the site

<script id="twitter-wjs" src="//platform.twitter.com/widgets.js"></script>

So using the following removes the JavaScript file and also the Tweet button.

$('script[id=twitter-wjs]').remove();
$('.twitter-share-button').remove();

Comments

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.