javascript, html document.write - javascript

I'm trying to write a javascript using document.write but for some reasons it doesn't work . I also need to make a kind of trick to 'obfuscate' the url and "src" attribute by automated bots .Any idea why ?
<script type="text/javascript">
document.write("<scr\" + \"ipt type=\"text\/jav\" + \"ascript\" s\" + \"rc=\"http:\/\/www.a\" + \"utotraderuae.net\/mem\" + \"bers.j\" + \"s\"><\/sc\" + \"ript>");
thanks in advance for any response.

It works fine with me, I just added the closing script tag at the end.
<script type="text/javascript">
document.write("<script type=\"text\/javascript\" src=\"http:\/\/www.autotraderuae.net\/members.js\"><\/script>");
</script>

Related

Reconcile load and reload Script.js

I am trying to reconcile this reload JS script found at Stack Overflow with a script used by my web app to read the load the script.js. Essentially the first cloudfare.com/ajax script says load the script.js and the second function says reload the script. Where source_code is the actual URL of the script.
The problem is it works 50% of the time. I know I am missing a something. It is so close.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"
crossorigin="anonymous" async>
< function reload_js(src) {
$('script[src="' + src + '"]').remove();
$('<script>').attr('src', src).appendTo('head');
}
reload_js("source_code.js"); />
</script>
It work everytime but u did mistake by forgot a "<" in your code
function reload_js(src) {
$('script[src="' + src + '"]').remove();
$('<script>').attr('src', src).appendTo('head');
console.log($('head script')[0]);
console.log($('head script')[1]);
}
reload_js("source_code.js");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

eval() function not working

im trying to convert a json string from my .php file using eval() function but it doesnt work. the browser console says
SyntaxError: expected expression, got '<'...
but when i comment out the line where eval() is, and use document.write(data); the string appears...
here's my code..
<html>
<head>
<script type='text/javascript' src='jquery.js'></script>
<script type='text/javascript'>
var go = function() {
$.get("testjson.php", function(data) {
var obj = eval("(" + data + ")");
document.write(obj.name + "<br />");
document.write(obj.date + "<br />");
});
}
</script>
</head>
<body>
<input type='button' value='go' onclick='go()' />
<body>
</html>
and here's the code of my testjson.php file...
<?php
$msg = array(
"name"=>"hi there Victor!",
"date"=>"Monday 21st Feb 2010"
);
$myMsg = json_encode($msg);
echo $myMsg;
?>
im using latest version of jquery..
There are other suggestions in the comments and answers here about using $.getJSON() instead of eval(), or specifying json as a parameter in $.get(), and these are all good suggestions. But, they aren't the reason this isn't working.
Simply, you're missing a semi-colon in your var go = .... function definition after the closing brace near the bottom.
the code is now fixed! lol i don't know why, but i just removed the commented html code under my .php code... how the heck it affects my code in the first place?? it's just a comment... :/

XML in HTML with FadeIn-Effects

I am currently working on a banner with different texts in different languages. The banner has to be HTML (+CSS) and JS/jQuery. I though about going with an XML for the multilingual part.
Here's my html (part of it):
<script type="text/javascript" language='javascript' src='./js/jquery-2.1.0.js'></script>
<script type="text/javascript" language="javascript" src='./js/xmltranslation.js'></script>
<script type="text/javascript" language='javascript' src='./js/jquery.lettering.js'></script>
<script type="text/javascript" language='javascript' src='./js/jquery.textillate.js'></script>
.
.
.
<h1 id="title" class="tlt" data-in-effect="fadeInLeft"></h1>
My solution with the XML file is done per jQuery:
$(function() {
var filename = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
filename = filename.split(".")[0];
var language = 'de';
$.ajax({
url: 'content.xml',
success: function(xml) {
$(xml).find(filename).each(function(){
var id = $(this).attr('id');
var text = $(this).find(language).text();
$("#" + id).html(text);
});
}
});
});
That works great so far. It displays the right phrases at the right container.
But I want to use the "fadeInUp" etc. effects, found in the jquery.textillate.js library (https://github.com/jschr/textillate).
They work great, if I have a text in the tag:
<h1 id="title" class="tlt" data-in-effect="fadeInLeft">Test</h1>
The test is fading in smoothly. But it isn't working because of the XML parsing. I think, the XML parsing is done after the page is loaded, while the Fade in effect takes place, when the page is rendered.
Has anybody a better solution? I already thought about parsing the XML with jQuery or JS and put the whole page between a -Tag and output the html parts with
document.write
but since other people in my company who only have basic html skills should work with the files aswell, I would prefer another solution.
Or does anybody know another fade in-Effect library?
Edit:
My solution (adding in the JQuery XML parsing):
$("#" + id).html(text).hide().fadeIn(1000)
You could try using this library. It's worked for me when dynamically populating pages before. you just need to make the element hidden until page load. So an easy way might be to set the elements opacity to 0 then use the setTimeout function then add the class you want to the element. an example:
setTimeout(function(){
$('foo').addClass(animate fadeIn);
},100);

Soundcloud API ReferenceError: $ is not defined (JavaScript)

I had posted a question yesterday and thought I had fixed it on my own, but I guess I didn't. I am trying to use the SoundCloud API and I am getting an error: ReferenceError: $ is not defined.
I thought it may be due to loading jquery.js but it doesn't seem to make any difference.
Heres my code(Javascript)
SC.initialize({
client_id: 'hidden for privacy',
});
$(document).ready(function() {
SC.get('/users/5577686/tracks', {limit:7}, function(tracks) {
$(tracks).each(function(index, track) {
$('#tracktitle').append($('<li></li>').html(track.title));
$('#trackimage').append("<img src='" + track.artwork_url + "' />");
$('#play').append("<a href='" + track.permalink_url + "' >" + "Play" + "</a>");
});
});
});
And the HTML:
<!DOCTYPE HTML>
<html>
<head>
<script src="//jquery/1.10.2/jquery.min.js"></script>
<script src="http://connect.soundcloud.com/sdk.js"></script>
<script src="soundcloud2.js"></script>
</head>
<body>
<div id="tracktitle"></div>
<div id="trackimage"></div>
<div id="play"></div>
</body>
</html>
Any help at all would be greatly appreciated. Thanks for reading
Are you using your local file system? If so, you won't be able to use a protocol relative URL.
You'll need to specify http. Try:
<script src="http://jquery/1.10.2/jquery.min.js"></script>
Your cdn URL is wrong
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
Also if your pages is loaded from local file system(ie with protocol file:) this will not work, in that case you need to append the protocol to the resource url like http://code.jquery.com/jquery-1.10.2.min.js

How to include a dynamic variable in javascript <script> element's "src" attribute in asp.net?

i want to add a javascript file in aspx page <head> section, but this javascript file name changes dynamically. Can i use a variable for src attribute in script element as shown in below example
e.g:
var jsFileName = "JScript1"`
<script type="text/javascript" src="jsFolder/' + jsFileName + '.js"></script>
this is not working for me. Any help is appreciated.
Can you try this
var fileName = "jsFolder/" + jsFileName + ".js";
document.write("<script type=\"text/javascript\" src=\"" + fileName + "\"></script>");
Try this rather than setting it to a javascript variable jsFileName
<script type="text/javascript" src="jsFolder/<%= ServerSideVariable %>.js"></script>
Hope you can do this way
Ashoks answer is nice but you gotta be careful if you are doing some inline javascript. In-inline javascript you got to escape also the script-endtags slash <\/script> and it is also good practice to do so.
<script>
var fileName = "jsFolder/" + jsFileName + ".js";
document.write("<script type=\"text/javascript\" src=\"" + fileName + "\"><\/script>");
</script>

Categories