Can anyone make a text(""); function? - javascript

I need help making a function that works like document.write(""); I need it to be called by using text(""); instead of printing out document.write(""); every single time. I just want to be able to use something other than document.write(""); every time. Here is what I have:
<script>
function text(a) {document.write("");}
text("Hi.");
</script>
But the function needs a parameter in the function to work. Can anyone help?
P.S. The real question is: "Can anyone help with making a text(""); function?"

Should be
<script>
function text(a) {document.write(a);}
text("Hi.");
</script>

Related

use javascript split function twice

I have the following string:
12.1.20.1, 81.32.68.68:50321
however I need to make it like this:
81.32.68.68
Could someone help me out with this?
I've already tried to use split(",")[1] but I also need to remove the :50321
Thank you!
If you can use the split function twice on it, you can do something like this:
console.log(
`12.1.20.1, 81.32.68.68:50321`
.split(", ")[1]
.split(":")[0]
);
This gives just the IP address out.
81.32.68.68

How to Pass Parameters from MVC controller to Javascript and get Result?

I have written a function in javascript which take 10 parameters as input and make some calculations on them and then return answer.
I am taking input from html page and returning result back to it.
Now i want to make API for it which takes input and directly call javascript function and return results.
I am using ASP .net MVC for this purpose as I am good in MVC development, but I am little bit confused that how can I call js function directly from MVC controller and get results there.
Can any one please help me.
<script type="text/javascript">
function myFunction() { // Calling this function on button click
var a = 's';
var b = 'Small';
var c ='55';
var d ='88';
getOutput(a,b,c,d); // My javascript function which make calculations and return results
};
</script>
Why not use the API to calculate whatever you are calculating, and only returning the answer to you. You could use AJAX for that. Or I might not understand your question good enough. In that case please elaborate.

Why isn't my array index displaying?

This seems so simple and I honestly can't see why this isn't working. Looked at other answers on here and mine still isn't working.
var randomArray = new Array();
randomArray[0] = 859;
alert(randomArray[0]);
How come nothing happens? It's probably an easy answer but I can't work it out.
Also, I'm trying to work out how to put a random number in the array so rather than putting 859 in index 0 lets say I want to put a random number between 1 and 20 in, so far I've got this but thats not working either
randomArray[1]=Math.floor(Math.random()*3);
EDIT: the .toString on alert seemed to fix it, thanks guys, knew it would be something small!
Is your javascript being referenced properly in a script tag with the correct type set?
<script type="text/javascript" ...> </script>
If not, it's fully possible your browser is simply ignoring it.
This is a wild guess, because your question doesn't contain enough information to know for sure, but make sure that your code is in a <script> block that looks like this:
<script>
// your code
</script>
Get rid of any "type" or "language" attribute on the tag; they're not needed and they're a source of error. If the "type" value is mispelled, then the browser will completely ignore the code.
Try calling .ToString() on your array property.
alert(randomArray[0].toString());

Passing Alphanumeric Parameter to javascript function

when i call a javascript function say, onclick="doSome(this.value)" where this.value=123, it works fine.
But if i call onclick="doSome(this.value) where this.value=A123, it is not working. How to rectify this problem?
I was also facing the same issue. I had to call a function with a product code as a parameter . It was working when the product code is numeric and it was not working for products like "AE11002".
I fixed the problem by changing the method invocation from
onClick="decreaseQuantity(${product.code})" to onclick='decreaseQuantity("${product.code}")'.
I think this will help you also. Please try this.

setInterval() issue in Mozilla (Javascript function)

I'm running through JavaScript: the Definitive Guide
It offers up the following code to explain setTimeout() and setInterval(), and my issue is that it runs in Safari without issue
but in Mozilla it doesn't seem to trigger at all, anyone have any
thoughts?
The issue is in the following function:
function invoke(f,start,interval,end){
if(!start) start=0; //default to 0ms (start right away)
if (arguments.length <= 2)
setTimeout(f,start);
It functions if I don't set the inverval and end, but if I do
something goes janky
else{
setTimeout(repeat,start);
function repeat(){
var h = setInterval(f,interval);
//if(end)setTimeout(function(){clearInterval(h)},end);
}
}
}
This is just the dummy function that runs on setTimeout() and
setInterval()
function f(){
if(true)
alert("yo");
}
<button onclick="invoke('f,200,1000,5000')">yo</button>
Hopfully somone has some insight into this one, thanks.
<button onclick="invoke('f,200,1000,5000')">yo</button>
should be
<button onclick="invoke(f,200,1000,5000)">yo</button>
Otherwise you are passing the string 'f,200,1000,5000' as the first parameter.
JSFiddle this now appear to work,
as the others have said you need to remove the 's arround your parameter to invoke
also FireBug for firefox ( get it if you dont already ) fails with Repeat is undefined so I've also modified that a little too.
It looks like you're passing a single variable to your invoke function due to the placement of your second single quote. Try changing it to
<button onclick="invoke('f',200,1000,5000)">yo</button>
and see if that works any better.

Categories