function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rose FarnhamRose Farnham 

How to add JavaScript via your browser's developer console?

I am new to Visualforce and I am doing the Trailhead: Visualforce Basics Module - Use Standard Controllers.  The instructions state:
To preview your page in the context of Lightning Experience, open your browser’s developer console and enter:
$A.get("e.force:navigateToURL").setParams(
    {"url": "/apex/pageName"}).fire();
I am not sure where to enter this.  When I enter it in the Visualforce page the text just show up on the Visualforce page preview.  I must be missing something.  Any help will be greatly appreciated.
 
Best Answer chosen by Rose Farnham
Alain CabonAlain Cabon
Hi,

I am using Chrome DevTools as browser's console.

To open the dedicated Console panel, either:
Press Ctrl+Shift+J (Windows / Linux) or Cmd+Opt+J (Mac).
If DevTools is already open, press the Console button.
When you open the Console panel, the Console drawer collapses automatically.

https://developers.google.com/web/tools/chrome-devtools/console/

https://trailhead.salesforce.com/fr/modules/visualforce_fundamentals/units/visualforce_creating_pages

The rendered page opens in a new window. Note that this page preview shows your page without Salesforce styling.

To see your page in the context of Lightning Experience, open your Chrome browser’s developer console and enter:

$A.get("e.force:navigateToURL").setParams( {"url": "/apex/HelloWorld"}).fire();
or
$A.get("e.force:navigateToURL").setParams( {"url": "/apex/HelloMobile"}).fire();

User-added image

Regards
Alain

All Answers

Alain CabonAlain Cabon
Hi,

I am using Chrome DevTools as browser's console.

To open the dedicated Console panel, either:
Press Ctrl+Shift+J (Windows / Linux) or Cmd+Opt+J (Mac).
If DevTools is already open, press the Console button.
When you open the Console panel, the Console drawer collapses automatically.

https://developers.google.com/web/tools/chrome-devtools/console/

https://trailhead.salesforce.com/fr/modules/visualforce_fundamentals/units/visualforce_creating_pages

The rendered page opens in a new window. Note that this page preview shows your page without Salesforce styling.

To see your page in the context of Lightning Experience, open your Chrome browser’s developer console and enter:

$A.get("e.force:navigateToURL").setParams( {"url": "/apex/HelloWorld"}).fire();
or
$A.get("e.force:navigateToURL").setParams( {"url": "/apex/HelloMobile"}).fire();

User-added image

Regards
Alain
This was selected as the best answer
Alain CabonAlain Cabon

User-added image

You have to "Switch to Lightning Experience" first.

Regards
 
Rose FarnhamRose Farnham
Hi Alaln,

Thanks you so much for your response to my questions.  I am currently using Firefox and with your response I am able to do it with Firefox.  I was unaware of the developer tools within the browsers itself. 
Yes, I was in Lightning Experience.
Have a great day!
Alain CabonAlain Cabon
Hi Rose,
Thanks Rose for having the good practice of closing the questions when they are solved.
Have a great day too!
Nitika Semwal 11Nitika Semwal 11
Hi, I also have same question. When I tried Alan solution in i am getting error as" $A is not defined at<anonymous> 1:1". Can someone help? I am doing this at my trailhead playground.
Yogesh BiyaniYogesh Biyani
@Alain  I too get  "$A is not defined at<anonymous> 1:1". What is $A ?
Skip KSkip K
"$A is not defined at<anonymous> 
I'm getting this too. Can someone help with this?
 
Judy SJudy S
Make sure you are in the Salesforce org browser when you execute the javascript.  It throws the same error for me when I execute it in the VF page preview browser.
Sitanshu TripathiSitanshu Tripathi
Step 1 - Open your salesforce org tab in Lightning Salesforce App.
Step 2 - Ctrl + Shift + J (From your keyboard)
Step 3 - Paste
              $A.get("e.force:navigateToURL").setParams(
              {"url": "/apex/pageName"}).fire();
Note - 1. Do not forget to change VF page name.
           2. Do not paste your code on your VF page preview tab otherwise it will throw error "$A is not defined at<anonymous> 1:1".
simonfischsimonfisch
$A.get("e.force:navigateToURL").setParams( {"url": "/apex/HelloWorld"}).fire();
when I add the above in firefox console I get an error "cant use '$A.get("e.force:navigateToURL").setParams('." and "Available Options Too many options". anyone an idea how to solve that?
Paul PostPaul Post
Uncaught ReferenceError: $A is not defined
    at <anonymous>:1:1
Sitanshu TripathiSitanshu Tripathi
Hi Paul, 
Please check my reply where is I also tell the Note that why you get this error.
Paul PostPaul Post
@Sitanshu Tripathi Thanks for the tip! Can't seem to understand why the trailhead explanation is so bad.
Denise CrosbyDenise Crosby
The trailhead explanations are terrible.
John ZhuJohn Zhu

Thank Judy S.  I've had the very same error of "$A is not defined" on the preview page when I ran the "$A" code there.  Instead, run the code on your Trailhead Playground home page where, I think, the needed lightening force platform has been loaded correctly.
Ángel Darío PaguayÁngel Darío Paguay
Thanks Sitanshu Tripathi, It was very useful!
Mrityunjaya TiwariMrityunjaya Tiwari
The $A is recognized on your org or app page "https://yourusername.lightning.force.com/one/one.app" or developer org. It will not work on the preview page of AccountSummary or ContactSummary "Visualforce page" you created in the developer console.

So you need to follow the steps given below to resolve the issue.
1. login on salesforce using your username and password.(if not already logged in or playground is not opened) Url:(https://login.salesforce.com/), use your trailhead credentails or dev org credential.

2. Above will open your dev org or palyground. (if it is not ligthtning then switch to lightning experience.).

3. press F12 in chrome window. 

4. click on console tab.

5. put the $A.get("e.force:navigateToURL").setParams(
    {"url": "/apex/AccountSummary"}).fire();
    //(Replace AccountSummary with the page/url you want to open).
6. press return/enter.

Note: $A is not recognized on the other pages because it's resources(libraries/assemblies/jars..) are not available there.
 
Yasantha ParanamannaYasantha Paranamanna
I was getting the  "$A is not defined salesforce" error. Thank you @Sitanshu Tripathi & @Mrityunjaya Tiwari for helping out!
Nipun KumarNipun Kumar

Also, Java Console needs to be enabled for the browser

Control Panel - Programs - Java - Advanced - Enable Java Console - Apply - OK

Franco DemareFranco Demare
Hi everyone!
When i use $A.get("e.force:navigateToURL").setParams(
    {"url": "/apex/HelloWorld"}).fire();  ,
it takes me to a home page instead of the HelloWorld vf page , does anyone know how to fix this?
thanks in advance!
Alvaro FloresAlvaro Flores
Thanks a lot for the help @Sitanshu Tripathi and @Mrityunjaya Tiwari, the instruction in Trailhead was really lame.