by espe
1 replies
Hi guys, I'm having a very difficult time trying to figure things up.
I'm making a script that should load content from another page within the same domain. I've already done this but it's not working because it also loads 'script' tags and executes those scripts and it shouldn't be that way...

let me give you an example of what I already got.


Code:
url = 'http://domain.com/content.html';

$.get(url)
.success(function(happy){

content = $(happy).find('.content');

$('#element').append(content);

 })

ok so I'm trying to load .content from (content.html) into #element BUT the problem is that content.html looks like this:

HTML Code:
<div class="content">
    <p>blah blah blah</p>
    <script>some_function(blah blah)</script>
</div>
<div class="content">
    <p>blah blah blah</p>
    <script>some_function(blah blah)</script>
</div>
<div class="content">
    <p>blah blah blah</p>
    <script>some_function(blah blah)</script>
</div>
so when its loaded into #element it looks like this:

HTML Code:
<div id="element">
    <div class="content">
        <p>blah blah blah</p>
        <script>some_function(blah blah)</script>
    </div>
    <div class="content">
        <p>blah blah blah</p>
        <script>some_function(blah blah)</script>
    </div>
    <div class="content">
        <p>blah blah blah</p>
        <script>some_function(blah blah)</script>
    </div>
</div>
and I want to remove those scripts BEFORE they get inserted!
I have tried the following:

Code:
url = 'http://domain.com/content.html';

$.get(url)
.success(function(happy){

content = $(happy).find('#content-parent').html().replace(/<script/g,' '); 
// side note: I know it doesnt remove the script but it kills it

$('#element').append(content);

 })
this works but it only loads the first .content element and there are several others that should also be loaded.

Please if you know the solution to this let me know! I know it's probably something stupid that I forgot but it's driving me craaazy, I've been 1 hour thinking why is this happening but no ideas come to my mind


EDIT: I found the solution to this problem here: prevent jquery ajax to execute javascript from script or html response - Stack Overflow just in case anyone else have this same issue.

Now back to coding!
#issue #jquery #small

Trending Topics