How to make <script></script> not appear in the HTML file. Only the text appears inside Document.write

Asked

Viewed 252 times

2

I’m trying to insert some scripts of my WHMCS on a page HTML on my website, which returns a document.write(' Texto que irá aparecer ');, and the text contained within document.write is displayed on the HTML page.

Tutorial Link: http://www.whmcs.blog.br/personalizacao/capturando-informacoes-do-produto/

To work, I have to insert several <script></script> which generates many requests.

I’d like to make it appear only:

<h1> Text that will appear </h1>

Instead of:

<h1><script language="javascript" src=" "> Text that will appear </script><h1>

Example in Jsfiddle

(used for example the Hostgator whmcs)


Is there any way to make only the text appear ?

  • Only with Javascript, or with jQuery as well?

  • Yes! It is worth working! D

  • 1

    You could implement a PHP script to serve as a proxy for the request, it is not very common but it can work. Avoiding doing multiple checks, asking the data only for a single URL and can still handle the result.

  • @Pedrohenrique Could you create an answer? Thank you!

  • The @Pedrohenrique comment really makes sense. How often do these data change? You could implement a script and call it via cronjob to update the data from time to time according to your need.

  • They never change, I think Cronjob is not a good alternative. @Kazzkiq you don’t know whmcs?

Show 1 more comment

2 answers

4


Using jQuery, you could do the following:

function removeScripts(el) {

    //Para cada elemento que deve ter o script removido
    $(el).each(function () {
        var i = this;

        //remove os espaços em branco
        var value = ($(i).text()).replace(/\s/g, '');

        //verifica se o texto já foi carregado dentro do elemento
        if (value.length > 0) {

            // se sim, remove a tag script do elemento
            $('script', i).remove();
        } else {

            // se não, dá mais 500ms e tenta denovo
            setTimeout(function () {
                removeScripts(el);
            }, 500);
        }
    });
}

And to call the function:

removeScripts('.elementos');

Example: FIDDLE

What this function does is to check on all the selectors you provided if there is any text (which would be the text loaded by the element script). If it exists, it only deletes the tag script, if it does not exist, this means that the script has not yet been able to load the text, so it waits 500 milliseconds and tries again.

0

Javascript is a language that must be executed in the client, and so it is not possible to completely hide a javascript code. In this case, the recommended is to separate the javascript code from the HTML, in a js file. Then this javascript file can be minified, being transformed into a more difficult file to be read by a human being. An example of website that can be used to minify your javascript code can be found here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.