JavaScript global function [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a function in a JavaScript file:
myscripts.js:
function myOn(node,event, selector, data, handler)
{
document.write("This text comes from an external script.");
}
and in the HTML file, I have this:
<body>
<script src="myscripts.js"></script>
...//some text and tags
<script>
myOn(outer, 'click','.deleteButton ', "", deleteDiv);
</script>
<body>
the function "myOn" don't run in the HTML file.
How i make this work?
I have searched the internet, but found some hard example for me to understand.
I need a simple example for beginner like me.

You have a syntax error in the function definition, there shouldn't be another parenthesis inside the parentheses
This line:
function myOn(node,event, selector, data, handler(eventObject))
should be:
function myOn(node,event, selector, data, handler)

Related

Where to put Error Alert in my JS [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Where I need to place Error Alert code in this JavaScript and if it is of any use in such JavaScript.
<script>
$(document).ready(function () {
$.get("ImagePreview?b_id=<%=id%>", function (responseJson) {
var $select = $("<div>").addClass("galleria").appendTo($("#imgas"));
$.each(responseJson, function (index, item) {
$("<img />").attr("src", item).appendTo($select);
});
});
});
</script>
Put an alert every odd line like alert("A"), alert("B") etc
Refresh the page
Remove every alert from your code and keep the one you like best
P.S: if you want to test the actual responseJson than alert is a bad idea.
Use console.dir(responseJson); inside your AJAX function.

How do I make every TR tag in a table a link with Perl and Javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If I have a tag called "post" and has href="something from perl" what should i use for javascript in order to make it clickable and redirects me to the link?
If you implement jQuery you can do something like this:
jQuery(document).ready(function($) {
$(".click").click(function() {
window.document.location = $(this).attr("href");
});
});
edit
assuming your tag looks like this
<tr class = "click" href = "<%= something from Perl %>">

Temporarily comment out <script> tag for debugging [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Often while coding and debugging I might want to comment out a <script> tag. An example might be when doing the following:
<script src="lib/jquery.js"></script>
<!--script src="lib/jquery.min.js"></script-->
I tend to add the new line instead of just changing the original to act as a reminder that I want to put that back before going live. I got this syntax from a colleague but I had never seen this it before. Is there a syntactically correct method to comment out <script> tags in HTML?
EDIT: I know there are lots of discussions about commenting out scripts in order to hide them from older browsers but that is not what I am doing. I am wanting to hide the tag completely.
One option would be to dynamically load your scripts in, given a debug flag. For example:
Markup:
<script src="lib/include.js"></script>
include.js
var IS_DEBUG = true;
if(IS_DEBUG) {
loadScript("jquery.js");
loadScript("anotherscript.js");
}
else {
loadScript("jquery.min.js");
loadScript("anotherscript.min.js");
}
function loadScript(name) {
var elem = document.createElement("script");
elem.src = name;
document.getElementsByTagName("head")[0].appendChild(elem);
}
That means you can just toggle the IS_DEBUG flag to load in the required scripts. This is a very rudimentary example, but you get the idea. You might even be able to tie this in with something like require.js

Can I place <script></script> inside <script></script> [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
So I need to load a advert contained in a set of script tags inside some script tags is it possible ?
When you use a <script> HTML tag what is inside is treated as a quoted (literal) string, so the inner </script> tag inside is treated as a closing tag rather than as a portion of the string. So you cannot directly use the tag inside a script section.
If you must, user2310289's approach is valid or you could also use string concatenation -
eg:
"</sc"+"ript>");
You could try some thing like
var x = '<' + '/script>'; // or
var x = '<\/script>';

Just wondering if this is poor JS coding practice [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have 2 separate functionalities on one page of my site, both of which call the same Ajax function. The 2 functionalities need to be distinguishable. My solution was to pass a string (called string here) in one of the functions, such as in the following simplified example.
<script type="text/javascript">
function AjaxFunction(string) {
if (string === 'String') {
alert('You have clicked Button2');
} else {
alert('You have clicked Button1');
}
}
</script>
<button type="button" id="Button1" onclick="AjaxFunction()">Click1</button>
<button type="button" id="Button2" onclick="AjaxFunction('String')">Click2</button>
This seems perfectly fine to me. The only issue is that string is null when Button1 is clicked. I imagine there is no problem with this since the if...else takes care of any ambiguity of what to do with string. I have tested with my actual Ajax function and everything works OK, but since I am still somewhat new to coding I always imagine that stupid mistakes I am not aware of will destroy the site. Sorry for the trivial question but am I missing any performance issues, bugs, etc. with this approach? Thank you for any help!
You need to pass an empty string into the first button call. So:
onclick="AjaxFunction('')"

Categories

Resources