HTML & C Programming - javascript

I am writing a dynamic webpage that is compiled and dynamically loaded using a C program. I have been writing the code by using:
printf ("<p><h1>Hello world!</h1></p>\n");
I want to add jQuery to the file, so the first thing I did was was a leader back slash to all quotes:
"Text here" becomes \"Text here\"
Now this leaves two problems:
1 - In the JS file, the original code looks like this:
*(?:(['\"])((?:\\\\.|[^\\\\])*?)\\3|("+O+")|)|)"+_+"*
And after the " correction is made looks like this (this is just a sample extract of code):
*(?:(['\\"])((?:\\\\.|[^\\\\])*?)\\3|(\"+O+\")|)|)\"+_+\"*
But a lot of this code is not greyed out (as it should be as I want just the contents of the line printed so the browser knows what to do). I have added some additional \s and made it grey - this appears to work, but still produces some compiling errors.
2 - When I attempt to compile, there are characters (such as \t , \r and /) that are not recongised by the compiler - how can I ensure that these are considered part of the javascript code?
Any help with this would be greatly appreciated.

You also need to escape the backslashes as well, not just the quotes, that should fix both your problems.
So your JS Code should look more like this in the end:
*(?:(['\\\"])((?:\\\\\\\\.|[^\\\\\\\\])*?)\\\\3|(\"+O+\")|)|)\"+_+\"*

Related

Add line break inside bookmarklet

I have a bookmarklet that allows me to wrap any function and insert a debugger statement before it is called.
It works fine, but since the bookmarklet is a single line debugger stops somewhere in the middle of a long string of code and I need to scroll to find the breakpoint.
How can I insert a new line character after the debugger statement so that when it's encountered the code in the console is split into two lines?
The \n and literal newline character copied from a textarea into the bookmarklet don't split lines in the debugger.
Generally, it is best to URL-encode a bookmarklet before storing in a bookmark. So you can have a multiline bookmarklet simply by encoding the newlines, as demonstrated by this: encodeURIComponent("alert('line1');\nalert('line2');")
That answers the question as you asked it, but I'm not sure if this is your real problem. If you provide example code I might be able to give further advice.
No, bookmarklets are always one-liners.
You can use the chrome debugger which has a "prettify code" option.
Another option would be not running it as a normal bookmarklet during debugging but injecting a script tag pointing to a properly formatted version of your script.

Javascript replace() function adding strange characters

Consider the following Javascript:
var previewImg = 'http://example.com/preview_img/hey.jpg';
var fullImg = previewImg.replace('preview','full');
I would expect the value of fullImg to be:
http://example.com/full_img/hey.jpg
In fact, it is... sort of. Running alert(fullImg); shows the expected url string. But when I deliver that variable to jQuery Fancybox, like this:
jQuery.fancybox.open(fullImg);
Something adds characters into the string, like this:
http://example.com/%EF%BF%BCfull_img/hey.jpg
Where is this %EF%BF%BC coming from? What is it? And most importantly, how do I get rid of it?
Some other clues: This is a Drupal 7 site, running jQuery 1.5.1. I'm using that same Fancybox script elsewhere on the site with no issues.
%EF%BF%BC is a sequence of three URL-encoded characters.
You clearly can't see any unexpected characters in the string. That's because the character sequence %EF%BF%BC is invisible.
It's actually a UTF-8 byte-order mark sequence. This sequence typically comes at the start of a UTF-8 encoded text file. They probably got into your code when you did a copy+paste from another file.
The quickest way to get rid of them is to find the bit of code that was copied+pasted, delete the characters on either side of the problem, and retype them. Depending on your editor, you may find the delete behaves strangely as it deletes the hidden characters.
Some text editors and IDEs will have an option to show hidden characters. If your editor has this, it may help you see where the mystery characters are so you can delete them.
Hope that helps.

Replace space with underscore in Netbeans Code Templates

I created the below JavaScript Code Template in Netbeans 7.1.1
This is to generate code automatically in the editor, instead of typing it.
do_this('${selection}${cursor}',13)
which will result in the below code, with the cursor between quotes
do_this('',13)
The template automatically places the text I have highlighted, between quotes.
Now, the problem: I would like to replace any spaces within the selected/highlighted piece of code, with underscores. I think this may be possible with Regular Expressions (regex), however I am not sure how to go about it.
Thanks
Not sure about the Netbeans specific stuff, but once you have the selection you could do something like this:
selection = selection.split(" ").join("_");
This is not possible in NetBeans Code Templates, as they do not have any functions to manipulate the data in variables. If I have a work around for it, I will post it here.
Thanks.

SyntaxHighlighter is acting funny

I am using SyntaxHighlighter on my website and it is acting very strange. I put my codes in <pre>s and used SyntaxHighlighter.all(), and what I got are in every single codes, it inserted 1~2 extra lines.
Where's the problem? I checked everything and everything is fine. Please help.
I don't know about SyntaxHighlighter, but in general, the <pre> tag works effective immediately, LF and CR everything. When Syntax Highlighter does its conversion, it might be respecting these line feeds as well. For example:
<pre>
There will be a line before and after this
</pre>
vs.
<pre>There will be no lines</pre>
We're used to writing well-formatted and well-indented code, but with pre tags you have to make an exception. Looking at your source code, I think you're doing it the top way.

My regex breaks when I "alert" it - because of escaped slashes?

I'm currently working on a CKEditor plugin which would add internal links to our CMS. One of the thing their current link plugin does is that it'll parse through a link when it loads the link dialog to figure out what "type" it is.
Since I created the internal type I need to add a regular expression to compare it to and I'm having trouble doing so. I managed to match my expression using this tool but once I use the same expression in the RegExp object definition it doesn't seem to work.
My links look like this:
/en/my_folder_5
or
/fr/my_folder_5
I tried the following (which worked in that tool):
/(en|fr)/[A-Za-z_^/]+_[0-9]+
but all the slashes get escaped when I "alert" the expression (which leads me to believe it might be what's breaking it since I copy pasted the alerted expression and it did not work)
Any help is appreciated :)
var regex = /\/(en|fr)\/[A-Za-z_^\/]+_[0-9]+/;
alert(regex.test('/fr/my_folder_5')); // prints true

Categories