v2.5.2
Giriş yap

ajax ile script kodu çalıştırma

hasaneryilmaz
665 defa görüntülendi

a.com/dosya.js bu dosyada


<script>
    alert("deneme");
</script>
<h1>Deneme</h1>

yazıyor.

b.com/dosya.html


<!doctype html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <title>blabla</title>

</head>
<body>
<div id="site">

</div>
<script>

    const ajax = axios.get("http://a.com/dosya.js").then(function (e){
        document.getElementById("site").innerHTML = e.data;
    });

</script>
</body>
</html>

burada çagırdığım a.com sitesinde bulunan alert kodu çalışmıyor sadece h1 kodu çalışıyor.ben ajax isteğinden gelen javascript kodlarını nasıl çalıştırabilirim.

Cevap yaz
Cevaplar (1)
sync
1069 gün önce
<script>
const s = document.createElement('script');
s.setAttribute('src','http://a.com/dosya.js');
document.head.appendChild(s);
</script>