Why does a function show up as not defined - javascript

XSL:
<script type="text/javascript">
<xsl:value-of select="concat('replaceAll(',$idS,');')"/>
</script>
HTML:
<script type="text/javascript">replaceAll(ID0ED);</script>
Javascript:
$(function () {
var cityNameRye = $("#spnCityID0ED").text().split(";")[1];
if (cityNameRye.toLowerCase() == "rye") {
//do something
}
});
I get the following error in the console which causes an issue in the site:
Uncaught ReferenceError: replaceAll is not defined
Can I create a dummy function which clears out the error?

Put this function code somewhere when it could be loaded before those from XSL are executed:
function replaceSpnCity(idPart) {
var idsToReplace = $("[id^='spnCity']"); //updated missing quote
idsToReplace.each(function() {
var id = $(this).attr('id');
var newId = id.replace(idPart,'');
$(this).attr('id',newId);
});
}
And call it from XSL like:
<xsl:value-of select="concat('replaceSpnCity(&apos;',$idS,'&apos;);')"/>

You do not have any function with the name replaceAll() in your code
try
function replaceAll(varName){
//code to execute
}
and see if you still have the error

Related

parse html code from json using native javascript

Is it possible to get out the code that is inside a json file? You can have a look at it here: http://www.bryzgalov.directadvert.ru/show.cgi?adp=768&json=4
What is the code or method should I use to put the html code inside any raw div?
I tried to use jsonp, but there is a mistake:
unexpected token <
<script>
function myFunction(data){
var arr = JSON.parse(data);
document.getElementById('advBlock').innerHTML = arr;
}
</script>
<script type="text/javascript" src="http://www.bryzgalov.directadvert.ru/show.cgi?adp=768&json=4&callback=myFunction"></script>
The JSONP data is already a Javascript object, not a JSON string, so you don't need to parse it:
<div id="advBlock"></div>
<script>
function myFunction(data) {
document.getElementById('advBlock').innerHTML = data;
}
</script>
<script src="http://www.bryzgalov.directadvert.ru/show.cgi?adp=768&json=4&callback=myFunction"></script>
<div id="advBlock"></div>
<script src="http://www.bryzgalov.directadvert.ru/show.cgi?adp=768&json=4&callback=myFunction"></script>
<script> function myFunction(data) { document.getElementById('advBlock').innerHTML = data; } </script>
You have to add the script before your function call.

Uncaught ReferenceError: jQuery is not defined

I don't see why I would get this, code below:
<head>
<script src="https://code.jquery.com/color/jquery.color-2.1.2.min.js" type="text/javascript"></script>
<script>
var json1 = {
text: "After first paragraph"
};
var first_content_added = false;
$(function() {
$(".learn-more").on("click", function() {
$.getJSON("json_info.json", function(data) {
appendContentToFirstP(data.reviews[0].about.moreinfo);
});
});
});
function appendContentToFirstP(content) {
if (first_content_added) {
return;
}
var after_first_p = $('<p class="more-info" />');
after_first_p.text(content);
$(".first").append(after_first_p);
first_content_added = true;
}
</script>
</head>
What would be causing the error?
My initial thought is the error is because I have not imported JQuery, but I have. It's inside the script tag at the top.
You haven't included jQuery, you've only included the plugin jQuery.color.
Reference it before jQuery.color:
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
write before that color js like this
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="https://code.jquery.com/color/jquery.color-2.1.2.min.js" type="text/javascript"></script>

use java script variable in ActionLink mvc3 razor

I have a textbox where i must to write a value, how to pass this value in my controller if I call my controller using ActionLink, this is my code:
view:
#Html.TextBox("tisch", "", new { #class = "teschChange"})
#Html.ActionLink("Apply Command", "ApplyCommand", "Work")
and this is te script
<script type="text/javascript">
$(function test() {
$(".teschChange").change(function () {
var tisch = $(this).attr('value');
});
});
</script>
I must to send tisch in ApplyCommandController
Thanks
Instead of using an action link, use a standard link:
<a id="l" href="#Url.Action("ApplyCommand", "Work")">Apply Command</a>
Then, you can use this script:
<script type="text/javascript">
$(function test() {
$(".teschChange").change(function () {
var tisch = $(this).attr('value');
$("#l").attr("href", "#Url.Action("ApplyCommand", "Work")/" + tisch);
});
});
</script>
Which will assign a href of: /Work/ApplyCommand/tisch.
this was the correct script:
$("#l").attr("href", "#Url.Action("ApplyCommand", "Work")?tisch=" + tisch);

Uncaught ReferenceError: msgbox is not defined (anonymous function)

//////////////////main.js file attached
function msgbox (title,text,type,time)
{
///////////////////////////////////////////
var img = "<img src='image/"+type+".png' /> ";
$("#window .wtext").html("<table border='0'><tr><td>"+img+"</td><td>"+text+"</td></tr></table>");
$("#window .wtitle").html(title);
///////////////////////////////////////////
//$("#window .wtext").css("height",(parseInt($("#window").css("height"),10)-65)+"px");
get_center("window");
///////////////////////////////////////////
$("#window").fadeIn();
if (time!=0)
{
var t = window.setInterval(function(){
$("#window").fadeOut();
window.clearInterval(t);
},time*1000);
}
}
//////////////////myajax.js file attached
function toggle_div ()
{
msgbox("title","text","ok",3);
}
I have problem when i call msgbox from myajax.js.
how can i use my function.
it is working from another file.
i should use something for declaring global function?
you need to add both main.js and myajax.js in to your html page within tag. Then you can call directly to your function.
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript" src="myajax.js"></script>

how to insert javascript code via jquery?

<div id="example">
</div>
<script type="text/javascript">
function insert() {
var data = '<script type="text/javascript"> function test() { a = 5; }<\/script>';
$("#example").append(data);
}
function get() {
var content = $("#example").html();
alert(content);
}
</script>
INSERT
GET
</body>
</html>
what i want to do:
when i click on insert, i want to insert this code into example div:
<script type="text/javascript"> function test() { a = 5; }<\/script>
when i click on get, i want to get that code, which i inserted there.
but when i click on insert, and then on get, there's no code.. where is problem ? thanks
According to your comment to Adam Bellaire, you want the script tag to display as normal text. What you are looking to do is encode the text with HTML entities, this will prevent the browser from processing it as normal HTML.
var enc = $('<div/>').text('<script type="text/javascript"> function test() { a = 5; }<\/script>').html();
$("#example").append(enc);
This works:
function insert() {
var data = '<script type="text/javascript"> function test() { a = 5; }<\/script>';
$('#example').text(data);
}
function get() {
var content = $('#example').text();
alert(content);
}
Are you checking for the code by browsing the live version of the DOM, using a tool like Firebug? If you are expecting to see your code rendered in your regular browser window, you won't, because the script tags are actually parsed when they are inserted, and script tags aren't visible elements in an HTML page.

Categories