What is inline javascript? [closed] - javascript

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 9 years ago.
Improve this question
Me and my programmer has 2 different views for what "Inline JavaScript" is.
I said inline JavaScript means JavaScript placed directly in the HTML file, without in a .JS file.
My programmer means inline JavaScript is JavaScript on 1 line, and like <button onclick="alert('test')">
I give him right in, that inline JavaScript also is "onclick='alert(...)" because it again is like my solution #1, all JavaScript loaded in HTML and not in JS.
Who's right?
We have a HTML file, and there is <script>....</script> JavaScript in the bottom, that is inline javascript, right?

A script tag without a src (ie. with code directly in the HTML document) is referred to as an inline script.
An onclick="..." attribute is called an inline event handler.

I've heard the term used for both of those, I don't think either of you is "wrong." I'd say I've heard it used more for #1 than for #2. Most terms I've heard for #2 are more clunky, like "onxyz event handler" or "inline event handler" (thank you Niet the Dark Absol for reminding me) or "DOM0 attribute event handler" or sometimes just "DOM0 handler."

Related

Possible to make an "alias" or "macro" to call a JavaScript function in HTML? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm writing a notebook in HTML for my own personal use. When writing an HTML document, you can't simply indent paragraphs with tabs or spaces. Rather than having to copy-and-paste several entities every time I want to indent a paragraph, I thought I'd be clever and write a JavaScript function to print 4 spaces. Unfortunately, I'll still end up copying and pasting <script>tab_function();</script> every time I want to indent something, which in that case I might as well just paste the entities instead.
I'm wondering if there is a way to create a "macro" or alias (like #define in C++) to make this less of a burden. It would be nice if I could #define "TAB" to call this function, but alas I don't know if such a thing exists in HTML. Is this even possible?
You can simply write a event listener into your html.
document.onkeydown = function(e) {
if(e.keyCode === 9){//9 = tab
//add 4 spaces, textarea.value += " "; perhaps?
e.preventDefault();
}
}
About macros, you can even sorta write your own compiler in javascript(compile to javascript I mean by that), by simply parsing text, basically anywhere.

Javascript: Replace Text in an Element [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've been working now for a while on the javascript process and can't really figure out on how to do this. I know how to basically tell javascript to set a certain value for a specific ID. But my Problem here is:
I'd like to write javascript to read out information (Server based, but that I'll do by myself ofc.) set it as var and then write it into text. It should look like this in the end:
Before writing (Without Javascript):
<h1 id="name" class="normal">Welcome, %Name%! What do you wish to do?</h1>
After writing (With Javascript):
<h1 id="name" class="normal">Welcome, John Connor! What do you wish to do?</h1>
So basically what I thought of is, that JS reads out information from server, then finds the h1 ID, searches in that text the %Name% value, and replaces it with the found name.
Please help me out here.Thanks!
Best Regards
Kodaigan
If using jQuery:
$('#name').html(function(index, html){
return html.replace('%Name%', yourVariable);
});
Pure javascript:
document.getElementById('name').innerHTML = document.getElementById('name').innerHTML.replace('%Name%','John');
This will replace a given string inside your h1 element.
If you have many paramaters, is better to use one of js lib's with templating.I prefere to use underscore.
http://underscorejs.org/#template. Also you can use if statements in youe template
How to use if statements in underscore.js templates?

loading jQuery at the end of the page for mobile [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've been told loading jQuery at the end of a page increases performance for mobile.
I don't believe this, however open for an explanation.
Regardless, is there any way to get jQuery calls at the beginning of the page to load only after jQuery has been loaded? I know of timeout work around but they seem inconsistent.
The reason why you get "better" performance is that the page will be parsed before it reaches JavaScript at the end of the document, and once a section is parsed, rendering can begin. By loading JavaScript at the end of the document, you let the basic hard-coded layout in your HTML and CSS appear before you add functionality with JavaScript. This gives the user the illusion of "faster loading time" for your page.
The caveat here is any JavaScript you want to use that will call on external libraries must occur after the libraries' script tags. This is not an issue if you have packed all of your code together into its own file, but it will cause trouble for inline scripts you have strewn about the page.
This is part of why such behavior is discouraged in production applications, the rest having to do with the ability to maximize compression of the script content with gzip and so on.

PHP: Detect if string contains JavaScript/jQuery code inside and delete that part of code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a variable in PHP which contains different HTML code. Sometimes i find in this HTML code parts of javascript code or jquery code. This extra code may or may not have the <script> tag. So for example i might find jquery functions surrounded in a <p> tag.
Is there a way to detect this javascript/jquery code in the string and delete?
Yes, you can use HTMLPurifier to scrub bad code out of user input.
http://htmlpurifier.org/

Add event on dynamic change of DIV [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a page (Facebook page) that has its content changing dynamically. I am writing a GM script but it only loads when the page refreshes, and this is not the case. I have tried to do something like:
mydiv.addEventListener('load', myfunc() ,false );
as I saw in a different thread, but it didn't work. What am I doing wrong?
Thanks.
Use setInterval() or the waitForKeyElements utility.
Can't give any more detail unless you provide specifics like: what, exactly, you are trying to do; before-and-after HTML; or even a link to the page and/or screenshots.
Change myfunc() to myfunc.
Using myfunc(), you are calling the function.
While myfunc is just passing the reference to your function.

Categories

Resources