Javascript:history.back(), but on a condition

I’ve read the threads on using this, and in fact have it working on a Button using this:

<a href="#" dmx-bind:href="javascript:history.back()" class="btn btn-primary mb-4">Back</a>

And we know we can use a condition on a dmx-on:click
For example:
dmx-on:click="browser1.viewport.width > 576 ? (selected_index.setValue(ID) + data_detail1.select(ID)) : (selected_index.setValue(ID) + data_detail1.select(ID) + offcanvas1.show())"
(this works fine, btw)

So I need to modify my button (or any other clickable item, doesn’t have to be this button) to check a variable and either:
javascript:history.back() -OR- goto a specific page/URL

Spent 30 minutes and can’t figure it out. I was trying to do a condition in the bind:href statement, but the condition kept getting a syntax error at the “:” . I just think a condition doesn’t work on dmx-bind:href
Any ideas?

Conditions work almost everywhere.

dmx-bind:href="var1.value == 'yes' ? 'https://some.url' : 'https://someother.url'"

What you can’t do is call JS functions in dmx. Anywhere.
So you need to create a function separately>

function goBack(){ javascript:history.back() }

And then NOT use href, and instead use dmx-on:click.
Here, use a flow to set the same condition and use RUN step for regular url browser.goto and RUN JS step to call goBack JS function.

1 Like

You should be perfectly fine doing:

dmx-bind:href="var1.value==1 ? 'javascript:history.back()' : 'https://myurl.com'"
2 Likes

thanks
got it finally
took an hour

i may go back and try this - but i may have had other issues.
thx

PS _ no more 350MB page crapping out your browser!
http://137.184.106.209/vehicle_images

1 Like

@Teodor, what is wrong with using the Browser component instead of using JavaScript? As in

var1.value==1 ? browser.goto(browser.referrer) : 'https://myurl.com'

There is nothing wrong :slight_smile: I am just answering the question of the user asked in his first post.

1 Like

This will not work for SPA from what I see. Its just returns ‘’ in SPA.

I didn’t realize the script is more of a URL. :+1:

@Robert_LM This is a better solution. If you can get it to work.

I’m trying to retain my position on a long page and a query parm.

http://137.184.106.209/vehicle_list?ev=SHOW

Will the “goto” do that? …or just redraw the page from scratch? The back() seems to work fine.

You can use the history.back() in this situation without any issues. You don’t need an additional component for this.