Javascript Address Bar Hack/Trick - javascript

We've all seen this:
javascript:document.body.contentEditable='true'; document.designMode='on'; void 0
But my question is, how does this actually work, surely if this code isnt in the source code, how does it have any effect when entered into the address bar?

Putting javascript: <anything> as a link or into the address bar will basically run the given piece of JS.
Instead of onclick or onmouseover you can just put Test if you really wanted.

The javascript: prefix instructs the browser to execute a script, rather than follow a link.
You probably know that you can alert a message like this:
Click me
Now, entering javascript:alert('test'); in the address bar of the browser is the exact same thing as following that link. Hence the code will be executed. It's a feature =)

It is a HTML5 specification and can be applied in the page itself or via adressbar like every javascript code... like a javascript bookmarklet from twitter.
Reference:
http://www.w3.org/TR/html5/editing.html#contenteditable

Related

How do I display a alert on a website?

I wanted to have a message pop up in the middle of the page of my website when there is a important announcement like the one below. Any ideas of what to use. I tries looking at the code but I have no idea how to get it to work. Any ideas will be appreciated.
Image (My Idea of how it should look)
It looks like you have tagged 'javascript' on this question, so I will show you the most common way to do it completely using javascript.
Use the alert() function. The alert function takes one parameter — the text that you want to show. All browsers will present this in a different way. This is really easy to do and is recommended especially if you are just starting.
An example of implementing this on your HTML website:
<script type="text/javascript">
alert('My message goes here');
</script>
If you want to change the way this looks, you will have to use an HTML modal. By using that, you can customize the HTML&CSS to look exactly like the picture you attached.
To get started with that, I recommend you check out W3Schools tutorial on this: https://www.w3schools.com/howto/howto_css_modals.asp

Simple URL to fire javascript

I need to fire this javascript function:
window.icegram.get_message_by_id(2072).show();
From a plain URL (e.g. www.google.com/? etc) as I cannot edit the HTML, as shown in this image:
I have tried to use this code in that URL field:
javascript:window.icegram.get_message_by_id(2072).show();
But when I click on the button, it says "Uncaught TypeError" and that the function is unknown.
For reference purposes, the Icegram documentation says that you can trigger the popup by using either using (both work on a Wordpress post):
Your text
OR
[icegram campaigns="XXXX"]Your Text [/icegram]
Maybe I could call the javascript function by including it in a separate file first? I would appreciate any help, I have tried and researched for a couple hours.
I can confirm that it adds the javascript section in the correct place -
does this mean that there is an issue with my javascript function, perhaps its not working properly? Confusing as it does work on a wordpress post (when using the full "a href=" code) but when I use the function it instantly says -
Alright, it seems that Icegram has a bug, in which the function ID used in the shortcode (e.g. 2072) is different from the function ID needed when calling using javascript (wtf), which in this case was 2073.
So if anyone else needs it, this is the solution for a plain URL field:
javascript:icegram.get_message_by_id(2073).show();
which results in the following output when viewed live on the frontend:
Your Anchor Text
I hope that helps someone. A big thank you to mplungjan and sabithpocker for the guidance and responses.

Jquery image bookmarklet not working in Django

Im working through Django By Example and in one chapter a Jquery bookmarklet is built within a Django app so that a user can easily save jpg images from a website into their user profile area within the Django app.
Im not an experienced JS or Jquery programmer but I did some JS some years back and can read the code however the tutorial does give exact instructions on what to do which I have followed and although I have managed to get the bookmarklet button to appear in my bookmarks bar in Chrome, nothing happens when I click it when browsing a webpage with jpg images.
This is my local Django dashboard where the bookmarklet button is added to the bookmarks bar and this part works fine
and this is what it should look like when clicked on, this is the part where nothing happens for me
these are the relevant js files
https://github.com/davejonesbkk/bookmarks/blob/master/images/templates/bookmarklet_launcher.js
https://github.com/davejonesbkk/bookmarks/blob/master/images/static/js/bookmarklet.js
the only thing I can see that is different with these compared to the files that came with the book is the indentation is a bit off but for some reason the indentation does seem to have changed a bit when I uploaded to Git and they dont look like that locally. Is indentation important in JS?
I followed the same book with the same examples but didn't had any trouble. Make sure your dashboard.html file is referring to the correct javascript file. If nothing works try to add the bookmark manually, you can see how that's done over here http://www.howtogeek.com/189358/beginner-geek-how-to-use-bookmarklets-on-any-device/ it'll sure to work.
And answer to your last question, Indentation is not as important in JavaScript as it's in Python, as python doesn't use any curly braces "{}" or semi-colons ";". But you can write your entire javascript code in a single line and it'll work because your using curly braces everywhere to tell which line of code ends where.
I agree with all the above. In addition, the following:
Error I noticed in the book:
In bookmarklet-launcher.js the js function being called from bookmarklet.js is called myBookmarklet(), however there is no function called this way in bookmarklet.js. So, you may want to use the same name in both js files.
Practically speaking however, the bookmarklet will always work because, not finding a myBookmarklet function in memory, bookmarklet-launcher.js appends the bookmarklet.js script to the body element and, being bookmarklet.js a self-invoking function, its content executed (without the need it to being called). There are some additional interesting technicalities here (the key function in bookmarklet.js is not self invoking but it will anyway be always called because of the script checking whether jQuery is present...) but ok, this is more relevant for those busy with the mentioned book (Django 2 by example).
Check whether bookmarkled, once you click on it, is added to the
current webpage:
2.1. Open devtools (F12 on Chrome) and check e.g. in the html head element whether you find the newly added link element containing the css attribute and/or in the body element whether you find the script element containing the reference to the bookmarklet.js file.
2.2. Alternative: Add an alert message on top of the bookmarklet.js script so that it will be launched if it is correctly loaded. Example:
(function(){
alert('bookmarkled loaded!');
var jquery_version =...
Make sure you're trying to use it on a HTTP site only. Since you're serving from same protocol. HTTPS site would always tell say: There is a problem loadingbyour jquery. That's how I solved mine.
dude.I have solved the problems I met like you.
The most important thing is that noticing the syntax error(without warnings),mainly caused by ignoring blank.
for example, in the line:
jQuery('#bookmarklet .images').append('<img src="'+image_url+'"/>');
between #bookmarklet and .images should lie a blank space,because of jquery syntax rules(meaning to search tag with id of bookmarklet and search tag with class equaling images within result previously).
Another two places worth notice are codes containing #bookmarklet .images a and #bookmarklet #close,requiring blank spaces between filter condition.
That's where I found I made mistaks mainly after studying syntax of jquery.
You'd better compare your codes with codes already loaded up to github by someone to make sure there are no more little errors(such as spelling).

Add javascript to cover page button

I have a button on my cover page and I need to assign to it 1 line of javascript code (namely javascript:languageClicked(0); to work with this neat addon: Multilingualizer) .
So the button should execute that line of code and do its usual job, namely to send the user to my main webpage.
thx in advance
Is the languageClicked function code from Multilingualizer? If so, maybe that addon isn't enabled correctly on your page or something and the js files for it are not being loaded.
But, once they are loaded. You can replace your button with this code:
ENTER
Screenshot
Since you want the user to be redirected to /expnew after activating languageClicked.
Hope this helps! Also, there is one catch though; don't change this unless you know for sure you have those JS libraries loaded. ^_^
Without some HTML snippets and the name of the button you are working with, it is hard to know exactly how to help. However, I can share some general knowledge that may provide the answer for you.
The two most common ways that JavaScript triggers are implemented are:
1. <a> "link buttons":
Your code might look something like:
Click me!
2. <input>/<button>/any element onclick events:
Your code might look something like:
<button onclick="javascript:languageClicked(0)">Click me!</button>

Html/Javascript - How to make an html link display a url, and have it actually be redirected to a javascript function?

I have an html page, and I need a link to show that the user would be going to 'example.html', when really, the link goes to 'javascipt:ajaxLoad(example.html);'.
I tried this:
Example
But it didn't work. Any help? I already asked the webmasters stackexchange, and they told me that this would be a javascript programming question. Not an html question.
Example
By returning false you prevent the default action. And this way the links will still work when javascript is disabled, but then you don't get the AJAX functionality.
Just point the href at the actual file. The javascript onclick will take precedence - as long as you take care to disable the actual click effect by doing a "return false" or similar, the status bar will show 'example.html' and not the javascript url.
As well, note that it should be javascript:... (you're missing an r). The onwhatever attributes are already assumed to be javascript, so you could just say onclick="ajaxLoad(...) anyways.
Look, I'm not sure if I got exactly what you're asking about here, but the following fix often works with me. Just change the double-quotes to single-quotes, and put double-quotes around the example.html part
<a href="example" onclick='javascipt:ajaxLoad("example.html");'>Example</a>

Categories

Resources