logo
down
shadow

How to Replace Text Within Script Tags Using JQuery


How to Replace Text Within Script Tags Using JQuery

Content Index :

How to Replace Text Within Script Tags Using JQuery
Tag : javascript , By : Vlad Sirenko
Date : January 11 2021, 03:34 PM

I think the issue was by ths following , You could use regular expression, remove the script tag and append a newly created one:
let scriptStr = $("#facebookPixel").html().replace(/content_ids: \[+(.*?)\]/, 'contents_id: [345, 567]');
$("#facebookPixel").remove();
let script = document.createElement('script');
script.id = "facebookPixel";
script.innerHTML = scriptStr;
$(document.head).append(script);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script id="facebookPixel">
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('set', 'autoConfig', false, '123123123');
fbq('track', 'Purchase', {
  content_ids: [456465456] //this is a random number
});
</script>

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Replace values in HTML thats inside <script> tags with Jquery


Tag : javascript , By : Lex Viatkine
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You can use the function callback version of jQuery's html and use a global string replace for the %%THEMEPATH%% part, and then (as you came up with) parse the HTML and handle the images in it before going back to source to update the script element text:
$("script.tmpl-popup").html(function(_, html) {
  // Update %%THEMEPATH%%
  html = html.replace(/%%THEMEPATH%%/g, "http://example.com");

  // Update src: `$.parseHTML` gives us an array of the top-level elements.
  // we loop through them, finding any `img[data-image]` that they contain,
  // and updating the `src` on those.
  return $.parseHTML(html).map(function(element) {
    // Note: If it were possible for an `img[data-image]` to be *at* the top
    // level, you'd need: `$(element).find("img[data-image]").addBack("img[data-image").attr...
    $(element).find("img[data-image]").attr("src", function() {
      return this.getAttribute("data-image"); // or $(this).attr("data-image") if you want more jQuery :-)
    });
    return element.outerHTML;
  });
});
$("script.tmpl-popup").html(function(_, html) {
  // Update %%THEMEPATH%%
  html = html.replace(/%%THEMEPATH%%/g, "http://example.com");
  
  // Update src: `$.parseHTML` gives us an array of the top-level elements.
  // we loop through them, finding any `img[data-image]` that they contain,
  // and updating the `src` on those.
  return $.parseHTML(html).map(function(element) {
    // Note: If it were possible for an `img[data-image]` to be *at* the top
    // level, you'd need: `$(element).find("img[data-image]").addBack("img[data-image").attr...
    $(element).find("img[data-image]").attr("src", function() {
      return this.getAttribute("data-image"); // or $(this).attr("data-image") if you want more jQuery :-)
    });
    return element.outerHTML;
  });
});
<p>You'll have to use right-click Inspect Element to see that the change has happened. :-)</p>
<script type="text-html" class="tmpl-popup"><div class="acf-fc-popup">
        <ul>
                                <li>
                    <a href="#" data-layout="one_column" data-min="" data-max=""><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-image="%%THEMEPATH%%/images/admin/layouts/one_column.png" /> <span>One Column</span></a>
                </li>
                                <li>
                    <a href="#" data-layout="two_columns" data-min="" data-max=""><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-image="%%THEMEPATH%%/images/admin/layouts/two_columns.png" /> <span>Two Columns</span></a>
                </li>
                        </ul>
        <a href="#" class="focus"></a>
    </div>
</script>
<script type="text-html" class="tmpl-popup"><div class="acf-fc-popup">
        <ul>
                                <li>
                    <a href="#" data-layout="one_column" data-min="" data-max=""><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-image="%%THEMEPATH%%/images/admin/layouts/one_column.png" /> <span>One Column</span></a>
                </li>
                                <li>
                    <a href="#" data-layout="two_columns" data-min="" data-max=""><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-image="%%THEMEPATH%%/images/admin/layouts/two_columns.png" /> <span>Two Columns</span></a>
                </li>
                        </ul>
        <a href="#" class="focus"></a>
    </div>
</script>
<script type="text-html" class="tmpl-popup"><div class="acf-fc-popup">
        <ul>
                                <li>
                    <a href="#" data-layout="one_column" data-min="" data-max=""><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-image="%%THEMEPATH%%/images/admin/layouts/one_column.png" /> <span>One Column</span></a>
                </li>
                                <li>
                    <a href="#" data-layout="two_columns" data-min="" data-max=""><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-image="%%THEMEPATH%%/images/admin/layouts/two_columns.png" /> <span>Two Columns</span></a>
                </li>
                        </ul>
        <a href="#" class="focus"></a>
    </div>
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

jquery replace only text in a div with other tags in it


Tag : jquery , By : mitry
Date : March 29 2020, 07:55 AM
it should still fix some issue I have a div that contains text and other tags in it
$('#Field').contents().filter(function() {
    return this.nodeType == 3
}).each(function(){
    this.textContent = this.textContent.replace('Text to replace','Hi I am replace');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Field"><span> </span>Text to replace<div ></div></div>

How to replace plain text with a specific value with the same text but <font> tags around it with JS or jQuery on


Tag : javascript , By : Tom D
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I have the following button: , The easiest would be to add a css style. Something like
button.sz-grid-dropdown.sz-grid-button {
  color: red;
}
var buttons = $("button").filter(function() {
  return (this.innerText === "SIZE 10");
});

How to replace text between two XML tags using jQuery or JavaScript?


Tag : javascript , By : user185144
Date : March 29 2020, 07:55 AM

replace all </script> tags with <\/script> in javascript or jquery


Tag : javascript , By : John Phipps
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Maximo JS automation script: "importPackage" is not defined
  • How to surround selected text inside textarea with <em> on key combination CTRL+i?
  • Your configured registry does not support audit requests ~ On npm audit
  • What's the Angular equivalent of Vue <slot/>?
  • Iconify icon not updating when "data-icon" set with setAttribute()
  • Javascript newbie stuck with XMLHttpRequest return data
  • How to add obj to to state in my example Redux React
  • Loading "grunt-karma.js" tasks...ERROR >> SyntaxError: Unexpected identifier
  • Why do I get log is not defined
  • get the multiple index of the same string
  • How can I iterate over an Array object by using For IN or FOR OF
  • Launch local executable from web browser
  • How to prepare array of json objects to d3 hierarchical tree structure
  • VueJS - event emitted - show HTML before other calculations
  • Export SASS/SCSS variables to Javascript without exporting them to CSS
  • How to attach an event handler only once continuously
  • How to make button open a link in new tab in JavaScript?
  • Find range from the javascript object
  • How can I use my jQuery code as JavaScript?
  • How to show 5 images in a 2 row slider with bxslider
  • How to convert stream into variables in typescript
  • Three.js custom shader error on Chrome GL_INVALID_OPERATION: Active draw buffers with missing fragment shader outputs
  • My JavaScript doesn't work. How can I solve this problem?
  • Why is this async function is running synchronously?
  • Get updated variable from within react hook before return
  • I want to show all image preview but whenever I upload a new one the old one is gone
  • Would I be able to give access to specific electron APIs safely?
  • Why several clicks fire for each instance of a Class?
  • cant grab value from div using attr()
  • Make web service not accessible by browser?
  • Select DOM elements within template
  • How to customize angular ui-grid row background color?
  • Binding data in two respective $scope in AngularJS, how?
  • react put some props after import class
  • Variable is distributed among same components
  • switch class in jQuery/JavaScript for active/inactive state
  • How To Track Outbound Clicks Using Javascript
  • How to get total seconds elapsed since the beginning of the month?
  • Protractor: Stale element reference at "browser.wait(EC.visibilityOf(confDial), FIFTY_SECONDS);"
  • Access to an array inside an object
  • Reseting an JavaScript Interval with different value?
  • Learning Angular for the first time - version 2 or 4?
  • Rendering RSS feed (XML) in React with jquery, why do object values parsed from the feed seem empty?
  • Node.js xml2js http.request tag matching
  • applying texture to custom plane geometry in Threejs
  • How do I pass an Event as a function parameter in HTML written inside JavaScript variable?
  • Interface of object parameter with default value and default property
  • Does using try / catch rather than `.catch` observable operator with XHR requests reduce performance?
  • In IE11 background colors not working on printing
  • TypeScript interface signature "(): string"
  • Is synchronous XMLHttpRequest supported in Internet Explorer 10 and 11?
  • Axios function not returning any values with js
  • How to use firestore TIMESTAMP to create time-stamp in JavaScript
  • Instantiate subclass without constructing
  • Outputing a created object in javascript to a server side JSON file
  • Transition the numeric labels in a bar chart
  • createBottomTabNavigator: hide just one tab from the tabBar
  • iife vs simple statement behavior with document.title
  • showDialogPopup Refresh Page after Click
  • Child component does not set the initial value passed from the parent: ReactJS
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com